Skip to content

67.二进制求和,重复判断 #21

Closed
@xiaxinandye

Description

@xiaxinandye

不需要在循环里再判断p1,p2是否大于等0了,循环条件就是p1 >=0 && p2 >=0。
while (p1 >= 0 && p2 >= 0) {
//carry += p1 >= 0 ? a.charAt(p1--) - '0' : 0;
//carry += p2 >= 0 ? b.charAt(p2--) - '0' : 0;
carry += a.charAt(p1--) - '0';
carry += b.charAt(p2--) - '0';
sb.insert(0, (char) (carry % 2 + '0'));
carry >>= 1;
}

注释的这两行代码,真的困扰了我 ~~,我都去复习运算符的顺序了,汗。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions