随笔分类 -  LeetCode

https://leetcode.com/ 按通过率由高到低做题
摘要:LeetCode 345 Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "he 阅读全文
posted @ 2016-04-26 09:34 Walker_Lee 阅读(223) 评论(0) 推荐(0)
摘要:LeetCode 278 First Bad Version// Forward declaration of isBadVersion API.bool isBadVersion(int version);int firstBadVersion(int n) { int start=1, e... 阅读全文
posted @ 2015-11-28 11:07 Walker_Lee 阅读(111) 评论(0) 推荐(0)
摘要:LeetCode 205 Isomorphic Strings用到哈希映射bool isIsomorphic(char* s, char* t) { char charArrS[256] = { 0 }; char charArrT[256] = { 0 }; int i = 0;... 阅读全文
posted @ 2015-11-28 10:46 Walker_Lee 阅读(165) 评论(0) 推荐(0)
摘要:LeetCode 88 Merge Sorted Array题目要求将nums2数组合并到nums1中(nums1的空间满足合并后的要求),可以考虑从后向前合并。void merge(int* nums1, int m, int* nums2, int n) { int i=m-1, j=n-... 阅读全文
posted @ 2015-11-28 10:14 Walker_Lee 阅读(130) 评论(0) 推荐(0)
摘要:LeetCode 58 Length of Last Wordint lengthOfLastWord(char* s) { int length=0,falg=0; int n=strlen(s); if(n=1;i--) { if(falg==1 && s[... 阅读全文
posted @ 2015-11-26 15:47 Walker_Lee 阅读(118) 评论(0) 推荐(0)
摘要:LeetCode 26 Remove Duplicates from Sorted Array与27题类似 http://www.cnblogs.com/walker-lee/p/4994950.htmlint removeDuplicates(int* nums, int numsSize) { ... 阅读全文
posted @ 2015-11-25 16:06 Walker_Lee 阅读(133) 评论(0) 推荐(0)
摘要:LeetCode 27 Remove ElementC语言实现:相当于新设置了一个指针n,仍用原有的存储空间,存放值不等于val的elementint removeElement(int* nums, int numsSize, int val) { int n=0; for(int i... 阅读全文
posted @ 2015-11-25 15:58 Walker_Lee 阅读(429) 评论(0) 推荐(0)
摘要:LeetCode 21 Merge Two Sorted Lists/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */stru... 阅读全文
posted @ 2015-11-23 10:25 Walker_Lee 阅读(111) 评论(0) 推荐(0)
摘要:LeetCode 232 Implement Queue using Stacksclass Queue { stack input, output;public: void push(int x) { input.push(x); } void pop(voi... 阅读全文
posted @ 2015-11-23 09:22 Walker_Lee 阅读(107) 评论(0) 推荐(0)
摘要:LeetCode 202 Happy Number主要用到了 如果循环里存在4则unhappybool isHappy(int n) { if(n==1) return true; if(n==4) return false; int sum=0; ... 阅读全文
posted @ 2015-11-19 09:31 Walker_Lee 阅读(136) 评论(0) 推荐(0)
摘要:LeetCode 263 Ugly Number很多人出现Time Limit Exceeded的情况,是由于没有判断num是否等于0.bool isUgly(int num) { if(num==0) return false; while(num%2 == 0) ... 阅读全文
posted @ 2015-11-18 16:43 Walker_Lee 阅读(141) 评论(0) 推荐(0)
摘要:LeetCode 83 Remove Duplicates from Sorted List/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next;... 阅读全文
posted @ 2015-11-17 10:24 Walker_Lee 阅读(115) 评论(0) 推荐(0)
摘要:LeetCode 70 Climbing Stairs使用递归方法,超时int climbStairs(int n) { if(n=2)int climbStairs(int n) { if (n == 0 || n == 1) return 1; int pre = 1... 阅读全文
posted @ 2015-11-17 09:53 Walker_Lee 阅读(119) 评论(0) 推荐(0)
摘要:LeetCode 206 Reverse Linked List递归:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */str... 阅读全文
posted @ 2015-11-17 09:22 Walker_Lee 阅读(124) 评论(0) 推荐(0)
摘要: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 阅读(116) 评论(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 阅读(92) 评论(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 阅读(154) 评论(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 阅读(147) 评论(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 阅读(127) 评论(0) 推荐(0)