zoukankan      html  css  js  c++  java
  • 手写代码 判断的知识点

    1- 在使用位运算时,不能直接把int 0 (0000) 作为while()的条件来判断。。

     1 public class Solution {
     2     public int NumberOf1(int n) {
     3         int result = 0;
     4         while(n){ //error:incompatible types: int cannot be converted to boolean
     5             n = n & (n-1);
     6             result++;
     7         }
     8         return result;
     9     }
    10 }

    还是需要用while(n!=0)来判断

    2- ==优先级大于&运算

    1 if(exponent & 1 == 1){
    2             return Helper(base, exponent/2)*Helper(base, exponent/2)*base;
    3         }

    因此会出现这个error:

    1 /Solution.java:21: error: bad operand types for binary operator '&'
    2 if(exponent & 1 == 1){
    3 ^
    4 first type: int
    5 second type: boolean

    因此,要修改为:

    1 if((exponent & 1) == 1){//加一个括号
    2             return Helper(base, exponent/2)*Helper(base, exponent/2)*base;
    3         }
  • 相关阅读:
    公司真题-字节跳动
    全素组探求
    枚举
    求n个整数的最大公约数
    Ajax技术
    读文本文件
    JSTL标签库
    URL跟URi的区别
    常用的JSTL标签
    EL表达语言
  • 原文地址:https://www.cnblogs.com/frankcui/p/10467961.html
Copyright © 2011-2022 走看看