Easy
UntilApril
这个作者很懒,什么都没留下…
展开
-
LeetCode#24 Swap Nodes in Pairs
key: head node & two pointers Runtime: 4 ms / beats 3.86% Reference:discuss/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)原创 2016-07-25 09:43:45 · 244 阅读 · 0 评论 -
LeetCode#171 Excel Sheet Column Number
key: math Runtime: 11 ms / beats 38.09% No referenceclass Solution { public: int titleToNumber(string s) { int col = 0; for(char i : s) { col = col * 26 + (i -原创 2016-08-13 11:36:00 · 376 阅读 · 0 评论 -
LeetCode#383 Ransom Note
key: map Runtime: 232 ms No referenceclass Solution { public: bool canConstruct(string ransomNote, string magazine) { bool res = true; map<char, int> m; for(auto i : magaz原创 2016-08-13 10:07:20 · 280 阅读 · 0 评论 -
LeetCode#237 Delete Node in a Linked List
key: Linked List Runtime: 16 ms / beats 15.47% No reference/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(原创 2016-08-10 17:55:56 · 281 阅读 · 0 评论 -
LeetCode#349 Intersection of Two Arrays
key: map–find & count Runtime: 16 ms / beats 19.77% No referenceclass Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> res; map<in原创 2016-08-10 16:55:46 · 246 阅读 · 0 评论 -
LeetCode#169 Majority Element
key: map Runtime: 44 ms/ beats 9.23% Reference: blogpublic: int majorityElement(vector<int>& nums) { map<int,int> mapcnt; map<int,int>::iterator iter; for(int i = 0; i < n原创 2016-08-05 14:34:59 · 300 阅读 · 0 评论 -
LeetCode#100 Same Tree
key: recursion Runtime: 0 ms / beats 17.62% No reference/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i原创 2016-07-29 15:57:51 · 296 阅读 · 0 评论 -
LeetCode#283 Move Zeroes
key: two pointers Runtime: 20 ms / beats 27.77% No referenceclass Solution { public: void moveZeroes(vector<int>& nums) { int cnt = 0; for(int i = 0; i < nums.size(); i++)原创 2016-07-29 11:26:00 · 194 阅读 · 0 评论 -
LeetCode#226 Invert Binary Tree
key: recursion Runtime: 0 ms / beats 22.92% No reference/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i原创 2016-07-29 10:48:21 · 214 阅读 · 0 评论 -
LeetCode#104 Maximum Depth of Binary Tree
key: recursion Runtime: 8 ms / beats 24.48% No reference/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(i原创 2016-07-29 10:40:11 · 321 阅读 · 0 评论 -
LeetCode#258 Add Digits
key: note that 9%9=0, and -1%9 = -1 Runtime: 8 ms/ beats 52.315 Reference: discussclass Solution { public: int addDigits(int num) { return 1+(num - 1)%9; } };原创 2016-07-29 09:34:29 · 259 阅读 · 0 评论 -
LeetCode#371 Sum of Two Integers
key: xor to get the sum, & to get the carry, use recursion Runtime: 0 ms/ beats 11.31% Reference:discussclass Solution { public: int getSum(int a, int b) { if(a == 0) return b原创 2016-07-28 18:20:58 · 210 阅读 · 0 评论 -
LeetCode#344 Reverse String
key: traverse from backward Runtime: 16 ms / beats 9.99% No referenceclass Solution { public: string reverseString(string s) { string res(s); for(int i = s.size()-1; i>=0; i--)原创 2016-07-25 10:05:40 · 248 阅读 · 0 评论 -
LeetCode#21 Merge Two Sorted Lists
key: judge if the lists end Runtime: 8 ms / beats 73.38% No reference/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : va原创 2016-07-23 17:56:06 · 240 阅读 · 0 评论 -
LeetCode#20 Valid Parentheses
key: stack & special condition Runtime: 0 ms / beats 15.90% No referenceclass Solution { public: bool isValid(string s) { if(s.size()%2 != 0) return false; stack<char>原创 2016-07-23 17:12:42 · 297 阅读 · 0 评论 -
LeetCode#19 Remove Nth Node From End of List
key: two pointers, one runs fast than the other n+1 steps Runtime: 4 ms / beats 37.25% Reference:discuss/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode原创 2016-07-23 15:53:00 · 271 阅读 · 0 评论 -
LeetCode#13 Roman to Integer
LeetCode#13 Roman to Integerkey: traverse the string from backwards Runtime: 44 ms / beats 52.85% Reference:discuss//I(1),X(10),C(100),M(1000),V(5),L(50),D(500) class Solution { public: int roman原创 2016-07-23 14:29:49 · 289 阅读 · 0 评论 -
LeetCode#168 Excel Sheet Column Title
key: here A to Z represent to 1 - 26 , so preform n - 1 in each loop to change it to 0 - 25 Runtime: 0 ms / beats 11.37% Reference:discussclass Solution { public: string convertToTitle(int n) {原创 2016-08-13 15:34:20 · 440 阅读 · 0 评论