随笔分类 - 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
阅读全文
摘要:LeetCode 278 First Bad Version// Forward declaration of isBadVersion API.bool isBadVersion(int version);int firstBadVersion(int n) { int start=1, e...
阅读全文
摘要:LeetCode 205 Isomorphic Strings用到哈希映射bool isIsomorphic(char* s, char* t) { char charArrS[256] = { 0 }; char charArrT[256] = { 0 }; int i = 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-...
阅读全文
摘要: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[...
阅读全文
摘要:LeetCode 26 Remove Duplicates from Sorted Array与27题类似 http://www.cnblogs.com/walker-lee/p/4994950.htmlint removeDuplicates(int* nums, int numsSize) { ...
阅读全文
摘要:LeetCode 27 Remove ElementC语言实现:相当于新设置了一个指针n,仍用原有的存储空间,存放值不等于val的elementint removeElement(int* nums, int numsSize, int val) { int n=0; for(int i...
阅读全文
摘要:LeetCode 21 Merge Two Sorted Lists/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */stru...
阅读全文
摘要:LeetCode 232 Implement Queue using Stacksclass Queue { stack input, output;public: void push(int x) { input.push(x); } void pop(voi...
阅读全文
摘要:LeetCode 202 Happy Number主要用到了 如果循环里存在4则unhappybool isHappy(int n) { if(n==1) return true; if(n==4) return false; int sum=0; ...
阅读全文
摘要:LeetCode 263 Ugly Number很多人出现Time Limit Exceeded的情况,是由于没有判断num是否等于0.bool isUgly(int num) { if(num==0) return false; while(num%2 == 0) ...
阅读全文
摘要:LeetCode 83 Remove Duplicates from Sorted List/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next;...
阅读全文
摘要: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...
阅读全文
摘要:LeetCode 206 Reverse Linked List递归:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */str...
阅读全文
摘要:LeetCode 169 Majority Element可以先排序,再return中位数,但估计这种方法不会被面试官认可。这里用到了Moore’s voting algorithm http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.h...
阅读全文
摘要:LeetCode 191 Number of 1 Bitshttps://en.wikipedia.org/wiki/Hamming_weightint hammingWeight(uint32_t n) { int num=0; while(n!=0) { n&=(...
阅读全文
摘要:LeetCode 235 Lowest Common Ancestor of a Binary Search Tree/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct Tr...
阅读全文
摘要:LeetCode 242 Valid Anagram由于都是小写字母,可以建立一个长度为26的数组来记录每个字母出现的次数。C语言实现如下:bool isAnagram(char* s, char* t) { if(strlen(s)!=strlen(t)) return 0; ...
阅读全文
摘要:LeetCode 171 Excel Sheet Column Numberc语言实现:int titleToNumber(char* s) { int num = 0; char c; while(c = *s++) { num = num*26 + c - ...
阅读全文
摘要:LeetCode 217 Contains Duplicate一种C++实现,先排序,再比较相邻值是否相等:class Solution {public: bool containsDuplicate(vector& nums) { std::sort(nums.begin(),...
阅读全文

浙公网安备 33010602011771号