摘要:Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return true; For n=5, return false; 思路: 知识点: n & (n - 1) 使得从右边数最后一个1变为
阅读全文
摘要:参考http://www.hawstein.com/posts/5.1.html 方案1:先将N中第0位到第i位保存下来(左闭右开:[0, i)),记作ret, 然后将N中第0位到第j位全清0([0, j]),通过向右移动j+1位然后再向左移动j+1位得到。 最后用上面清0后的值或上(m«i)再或上
阅读全文
摘要:方法二: num & (num -1) 一直就把最后一个非0位变成1, 一直变到所有的都是0, 那么process了几次, 就是有几位1
阅读全文
摘要:/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } *
阅读全文
摘要:class Solution { /** * @return: The same instance of this class every time */ //http://www.cnblogs.com/EdwardLiu/p/4443230.html ...
阅读全文
摘要:Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, return 2 Given 1023, return 9 Count how many 1 in b
阅读全文
摘要:public class Solution { /** * @param string: An array of Char * @param length: The true length of the string * @return: The true length...
阅读全文
摘要:1) Non-recursive: class Solution { /** * @param n: an integer * @return an integer f(n) */ public int fibonacci(int n) { // ...
阅读全文