LintCode A + B Problem】的更多相关文章

原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是res中的对应位. carry 更新为0还是1要分别讨论. Time Complexity: O(1), 一共32位. Space: O(1). AC Java: class Solution { /* * param a: The first integer * param b: The second…
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and b…
* Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an integer * @return an integer */ int postOffice(vector<int>& A, int k) { int n = A.size(); sort(A.begin(), A.end()); // Cost btw. 2 houses i-j wit…
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and b…
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ pub…
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩下一个元素的时候(事实上只要满足一个元素出现过半就一定会剩下一个元素的)这个元素就是我们要找的数了. AC代码: public class Solution { /** * @param nums: a list of integers * @return: find a majority numb…
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ pu…
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还是只做出了O(n^2)... 勉强AC代码: public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String s) { for(int i=0;i<s.…
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left…
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param node: the node…