摘要: LeetCode 169 Majority Element可以先排序,再return中位数,但估计这种方法不会被面试官认可。这里用到了Moore’s voting algorithm http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.h... 阅读全文
posted @ 2015-11-16 09:44 Walker_Lee 阅读(114) 评论(0) 推荐(0)
摘要: LeetCode 191 Number of 1 Bitshttps://en.wikipedia.org/wiki/Hamming_weightint hammingWeight(uint32_t n) { int num=0; while(n!=0) { n&=(... 阅读全文
posted @ 2015-11-16 09:16 Walker_Lee 阅读(89) 评论(0) 推荐(0)
摘要: LeetCode 235 Lowest Common Ancestor of a Binary Search Tree/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct Tr... 阅读全文
posted @ 2015-11-16 09:07 Walker_Lee 阅读(153) 评论(0) 推荐(0)
摘要: LeetCode 242 Valid Anagram由于都是小写字母,可以建立一个长度为26的数组来记录每个字母出现的次数。C语言实现如下:bool isAnagram(char* s, char* t) { if(strlen(s)!=strlen(t)) return 0; ... 阅读全文
posted @ 2015-11-14 10:17 Walker_Lee 阅读(146) 评论(0) 推荐(0)
摘要: LeetCode 171 Excel Sheet Column Numberc语言实现:int titleToNumber(char* s) { int num = 0; char c; while(c = *s++) { num = num*26 + c - ... 阅读全文
posted @ 2015-11-14 09:30 Walker_Lee 阅读(114) 评论(0) 推荐(0)
摘要: LeetCode 217 Contains Duplicate一种C++实现,先排序,再比较相邻值是否相等:class Solution {public: bool containsDuplicate(vector& nums) { std::sort(nums.begin(),... 阅读全文
posted @ 2015-11-14 09:07 Walker_Lee 阅读(126) 评论(0) 推荐(0)
摘要: leetcode 226 Invert Binary Tree递归方法/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * stru... 阅读全文
posted @ 2015-10-17 16:28 Walker_Lee 阅读(122) 评论(0) 推荐(0)
摘要: LeetCode 283 Move Zeroesvoid moveZeroes(int* nums, int numsSize) { int i=0,j=0; for(i;ij) j=i; }} 阅读全文
posted @ 2015-10-17 16:12 Walker_Lee 阅读(115) 评论(0) 推荐(0)
摘要: LeetCode 100 Same Tree递归方法:/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeN... 阅读全文
posted @ 2015-10-17 10:51 Walker_Lee 阅读(100) 评论(0) 推荐(0)
摘要: LeetCode 237 Delete Node in a Linked List/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * };... 阅读全文
posted @ 2015-10-17 10:21 Walker_Lee 阅读(89) 评论(0) 推荐(0)