- 博客(14)
- 收藏
- 关注
原创 leetcode 109: Convert Sorted List to Binary Search Tree
Use the fast slow method to find the mid point of the list and make it the root. Then do it recursively on both sides. /** * Definition for singly-linked list. * struct ListNode { * int val;
2015-09-22 09:51:44 411
原创 leetcode 57: Insert Interval
Loop the array once and merge the newInterval with every intervals. If it cannot be merged into any interval, just insert. /** * Definition for an interval. * struct Interval { * int start; *
2015-09-15 08:04:52 238
原创 leetcode 9: Palindrome Number
Reverse the number and compare it with the original one. class Solution { public: bool isPalindrome(int x) { int old_x=x; int new_x=0; while(x>0) { if(
2015-09-02 21:00:20 209
原创 leetcode 8: Sting to Integer (atoi)
class Solution { public: int myAtoi(string str) { int len=str.length(); int i=0; while(i<len&&str[i]==' ') i++; str=str.substr(i);//erase all white spac
2015-09-02 20:41:12 408
原创 leetcode 7: Reverse Integer
class Solution { public: int reverse(int x) { int sign=1; if(x<0) { sign=-1; x=-x; } string s; while(x) {
2015-09-02 12:34:45 229
原创 leetcode 6: ZigZag Conversion
In one line, the first character is s[i], the second is s[i+2*numRows-2-2*i] and the third is s[i+2*numRows-2]. Just be careful with the test cases that numRows==1 and numRows>len. class Solution { p
2015-09-02 12:05:19 259
原创 leetcode 5: Longest Palindrome Substring
Use DP. dp[i][j] means whether the substring i to j is a palindrome. class Solution { public: string longestPalindrome(string s) { int len=s.length(); bool dp[len][len]; i
2015-09-01 22:57:23 315
原创 leetcode 4: Median of Two Sorted Arrays
Use the idea of merge sort. class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int m=nums1.size(); int n=nums2.size(); int i=0,j=0,curr
2015-09-01 22:20:55 266
原创 leetcode 3: Longest Substring Without Repeating Characters
Use a window to get a substring. The end of the window keeps forward and once it meets a character that is already in the window, update the start of the window. The unordered map can be replaced by a
2015-09-01 19:04:06 228
原创 leetcode 2: Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* ad
2015-09-01 18:41:42 243
原创 leetcode 1: Two Sum
Use a unordered map to save all visited number. While scan the array, find out whether target-nums[i] is already visited. class Solution { public: vector twoSum(vector& nums, int target) {
2015-09-01 18:00:47 227
原创 leetcode 241: Different Ways to Add Parentheses
Learn from https://leetcode.com/discuss/48488/c-4ms-recursive-%26-dp-solution-with-brief-explanation. Recursion method, find every operation character and divide the string according to them. class
2015-09-01 13:37:49 218
原创 leetcode 240: Search a 2D Matirx II
Use binary search for those rows that might contains the target. class Solution { public: bool searchMatrix(vector>& matrix, int target) { if(matrix.empty()) return false;
2015-09-01 11:24:56 271
原创 leetcode 239: Sliding Window Maximum
Use the a deque to save numbers like a waitlist. If the window size equals to k, start the pop operation of the deque and push the front number into the res vector. If nums[i-k] equals to the front nu
2015-09-01 00:19:17 230
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人