思路:
例如:
a=5,b=9,a+b=14
a转换为二进制形式为101,b转换为二进制形式为1001,其和转换为二进制形式为1110。
对于二进制形式的相加,可分两步进行操作:
1)先不考虑进位,则0101+1001=1100,从中可以看出,不考虑进位求和即对两个加数进行按位异或操作。
2)再考虑进位,则0101+1001=1110=1100+0010,即第一步所得结果再加上进位0010,而0010可通过下述方法进行计算:两个加数进行按为与操作得到0001,再将0001左移一位得到0010。
采用递归思想,重复进行上述两步操作,直至无进位,可实现不使用加减乘除进行加法操作。
代码:

225611384

Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For exa...

阅读全文

Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not ...

阅读全文

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space...

阅读全文