BM371SumOfTwoIntegers

silu

  • By using bit of & to get the bits that should be incremented
    • Find the two logic errors on the second time solve it, without see other’s solutions
    • Special cases for negative integers! don’t forget it! See Negative Integer Bit Summary

Negative Integer


  • Luckily my algs works for one negative one positive, but does not work for two negatives.
  • Why it’s ok for one negative one positive?
  • I tried to find it in the format of negative integer by bit representation. However, then I find my while condition is increase > 0 which actually is increase != 0 in my mind. I ignore the negative inputs and then ignore the conditions in my code.
  • Next time: if find ignore some input cases, check if missing any condition cases also.

Very good negative integer bits manipulation summary
https://discuss.leetcode.com/topic/49771/java-simple-easy-understand-solution-with-explanation

Logic Loopholes

  • In my first time solve it, I want to make sum = a&b; sum &= ~increase to get rid of the 1 in intersection . It turned out that ~increase is 32 bits long, which is not wise.
  • When both numbers have 1 in the same position, after added, it should turned to 0 instead of 1. So, we can use a^b instead of a&b
  • When updating the sum and increase, they should all use the old values. Pay attention not use their updated values.

Failed Cases

  • -12 -8 Don’t forget negatives
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值