手写代码 - 判断的知识点
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 }
分类:
手写代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?