From 85910c51f26780f722c368dfe1f08f47806d6770 Mon Sep 17 00:00:00 2001 From: huihut Date: Mon, 16 Apr 2018 22:30:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4LeetcodeProblems?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Problems/LeetcodeProblems/1-two-sum.h | 18 ---------- .../11-container-with-most-water.h | 22 ------------ .../LeetcodeProblems/118-pascals-triangle.h | 21 ------------ .../119-pascals-triangle-ii.h | 13 ------- .../121-best-time-to-buy-and-sell-stock.h | 14 -------- .../122-best-time-to-buy-and-sell-stock-ii.h | 26 -------------- .../LeetcodeProblems/169-majority-element.h | 26 -------------- .../26-remove-duplicates-from-sorted-array.h | 14 -------- Problems/LeetcodeProblems/283-move-zeroes.h | 12 ------- .../4-median-of-two-sorted-arrays.h | 34 ------------------- .../LeetcodeProblems/53-maximum-subarray.h | 13 ------- Problems/LeetcodeProblems/66-plus-one.h | 15 -------- .../LeetcodeProblems/88-merge-sorted-array.h | 20 ----------- 13 files changed, 248 deletions(-) delete mode 100644 Problems/LeetcodeProblems/1-two-sum.h delete mode 100644 Problems/LeetcodeProblems/11-container-with-most-water.h delete mode 100644 Problems/LeetcodeProblems/118-pascals-triangle.h delete mode 100644 Problems/LeetcodeProblems/119-pascals-triangle-ii.h delete mode 100644 Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h delete mode 100644 Problems/LeetcodeProblems/122-best-time-to-buy-and-sell-stock-ii.h delete mode 100644 Problems/LeetcodeProblems/169-majority-element.h delete mode 100644 Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h delete mode 100644 Problems/LeetcodeProblems/283-move-zeroes.h delete mode 100644 Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h delete mode 100644 Problems/LeetcodeProblems/53-maximum-subarray.h delete mode 100644 Problems/LeetcodeProblems/66-plus-one.h delete mode 100644 Problems/LeetcodeProblems/88-merge-sorted-array.h diff --git a/Problems/LeetcodeProblems/1-two-sum.h b/Problems/LeetcodeProblems/1-two-sum.h deleted file mode 100644 index 4bfcb23..0000000 --- a/Problems/LeetcodeProblems/1-two-sum.h +++ /dev/null @@ -1,18 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - vector sum; - for(auto i = 0; i < nums.size(); i++) - { - for(auto j = i+1; j < nums.size(); j++) - { - if(i == j) continue; - if(nums[i] + nums[j] != target) continue; - sum.push_back(i); - sum.push_back(j); - return sum; - } - } - return sum; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/11-container-with-most-water.h b/Problems/LeetcodeProblems/11-container-with-most-water.h deleted file mode 100644 index 54d7f67..0000000 --- a/Problems/LeetcodeProblems/11-container-with-most-water.h +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { -public: - int maxArea(vector& height) { - int left = 0, right = height.size()-1; - int max = 0, temp = 0; - while(left < right) { - temp = (right - left) * (height[left] < height[right] ? height[left] : height[right]); - if(max < temp) max = temp; - if(height[left] < height[right]) { - do{ - left++; - } while(left < right && height[left-1] >= height[left]); - } - else { - do{ - right--; - } while(left < right && height[right] <= height[right+1]); - } - } - return max; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/118-pascals-triangle.h b/Problems/LeetcodeProblems/118-pascals-triangle.h deleted file mode 100644 index 3d89970..0000000 --- a/Problems/LeetcodeProblems/118-pascals-triangle.h +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { -public: - vector> generate(int numRows) { - vector> triangle; - for(int i = 0; i < numRows; i++){ - vector v; - if(i==0){ - v.push_back(1); - } - else{ - v.push_back(1); - for(int j = 0; j < triangle[i-1].size()-1; j++){ - v.push_back(triangle[i-1][j] + triangle[i-1][j+1]); - } - v.push_back(1); - } - triangle.push_back(v); - } - return triangle; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/119-pascals-triangle-ii.h b/Problems/LeetcodeProblems/119-pascals-triangle-ii.h deleted file mode 100644 index 2cb0046..0000000 --- a/Problems/LeetcodeProblems/119-pascals-triangle-ii.h +++ /dev/null @@ -1,13 +0,0 @@ -class Solution { -public: - vector getRow(int rowIndex) { - vector v(rowIndex + 1, 0); - v[0] = 1; - for (int i = 0; i < rowIndex; i++){ - for(int j = i + 1; j > 0; j--){ - v[j] += v[j - 1]; - } - } - return v; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h b/Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h deleted file mode 100644 index fef96f9..0000000 --- a/Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - int maxProfit(vector& prices) { - int left = 0, right = 0; - int max = 0, temp = 0; - for(auto i = 0; i < prices.size(); ++i) { - right = i; - temp = prices[right] - prices[left]; - if(temp < 0) left = i; - if(max < temp) max = temp; - } - return max; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/122-best-time-to-buy-and-sell-stock-ii.h b/Problems/LeetcodeProblems/122-best-time-to-buy-and-sell-stock-ii.h deleted file mode 100644 index 24a9a8d..0000000 --- a/Problems/LeetcodeProblems/122-best-time-to-buy-and-sell-stock-ii.h +++ /dev/null @@ -1,26 +0,0 @@ -class Solution { -public: - int maxProfit(vector& prices) { - int max=0, begin=0, end=0; - bool up=false, down=false; - for (int i=1; i prices[i-1] && up==false){ // goes up - begin = i-1; - up = true; - down = false; - } - if (prices[i] < prices[i-1] && down==false) { // goes down - end = i-1; - down = true; - up = false; - max += (prices[end] - prices[begin]); - } - } - // edge case - if (begin < prices.size() && up==true){ - end = prices.size() - 1; - max += (prices[end] - prices[begin]); - } - return max; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/169-majority-element.h b/Problems/LeetcodeProblems/169-majority-element.h deleted file mode 100644 index 899de42..0000000 --- a/Problems/LeetcodeProblems/169-majority-element.h +++ /dev/null @@ -1,26 +0,0 @@ -#include -class Solution { -public: - int majorityElement(vector& nums) { - int n = nums.size(); - map m_times; - int moreThanTimes = n / 2; - for (auto i = 0; i < n; ++i) { - auto it = m_times.find(nums[i]); - if (it == m_times.end()) { - // No existence in map, insert - m_times.insert(std::pair(nums[i], 1)); - it = m_times.find(nums[i]); - } - // Is it more than ⌊ n/2 ⌋ times ? - if ((*it).second > moreThanTimes) { - return (*it).first; - } - else { - (*it).second++; - } - } - // Can't find - return -1; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h b/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h deleted file mode 100644 index 2ce7bb6..0000000 --- a/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - int removeDuplicates(vector& nums) { - int n = nums.size(), pos = 0; - if(n <= 1) - return n; - for(int i = 0; i < n-1; ++i) { - if(nums[i] != nums[i+1]) { - nums[++pos] = nums[i+1]; - } - } - return pos+1; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/283-move-zeroes.h b/Problems/LeetcodeProblems/283-move-zeroes.h deleted file mode 100644 index 19059f1..0000000 --- a/Problems/LeetcodeProblems/283-move-zeroes.h +++ /dev/null @@ -1,12 +0,0 @@ -class Solution { -public: - void moveZeroes(vector& nums) { - int left = 0, right = 0; - for(; left < nums.size() && nums[left] != 0; left++); - for(right = left; right < nums.size(); right++) { - if(nums[right] == 0) continue; - nums[left++] = nums[right]; - } - while(left < nums.size()) nums[left++] = 0; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h b/Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h deleted file mode 100644 index 57fd986..0000000 --- a/Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h +++ /dev/null @@ -1,34 +0,0 @@ -class Solution { -public: - double findMedianSortedArrays(vector& nums1, vector& nums2) { - int len1 = nums1.size(); - int len2 = nums2.size(); - int len = len1 + len2; - if(len & 1) - return findKth(nums1, 0, nums2, 0, len / 2 + 1); - else - return (findKth(nums1, 0, nums2, 0, len / 2) + findKth(nums1, 0, nums2, 0, len / 2 + 1)) / 2; - } - - // find kth number of two sorted array - double findKth(vector& nums1, int i1, vector& nums2, int i2, int k) { - if (i1 >= nums1.size()) { - return nums2[i2 + k - 1]; - } - if (i2 >= nums2.size()) { - return nums1[i1 + k - 1]; - } - if (k == 1) { - return min(nums1[i1], nums2[i2]); - } - int key1 = i1 + k / 2 - 1 >= nums1.size() ? INT_MAX : nums1[i1 + k / 2 - 1]; - int key2 = i2 + k / 2 - 1 >= nums2.size() ? INT_MAX : nums2[i2 + k / 2 - 1]; - if (key1& nums) { - int max = MIN_INT, temp = 0; - for(auto i = 0; i < nums.size(); ++i) { - temp += nums[i]; - if(temp > max) max = temp; - if(temp < 0) temp = 0; - } - return max; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/66-plus-one.h b/Problems/LeetcodeProblems/66-plus-one.h deleted file mode 100644 index 4aee315..0000000 --- a/Problems/LeetcodeProblems/66-plus-one.h +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - vector plusOne(vector& digits) { - int carry = 1; - for(int i = digits.size() - 1; i >= 0; --i) { - int add = digits[i] + carry; - digits[i] = add % 10; - carry = add / 10; - } - if(carry > 0) { - digits.insert(digits.begin(), carry); - } - return digits; - } -}; \ No newline at end of file diff --git a/Problems/LeetcodeProblems/88-merge-sorted-array.h b/Problems/LeetcodeProblems/88-merge-sorted-array.h deleted file mode 100644 index 19e222e..0000000 --- a/Problems/LeetcodeProblems/88-merge-sorted-array.h +++ /dev/null @@ -1,20 +0,0 @@ -class Solution { -public: - void merge(vector& nums1, int m, vector& nums2, int n) { - int i1 = m - 1, i2 = n - 1; - for(int i = m + n - 1; i >= 0; i--) { - if(i1 >= 0 && i2 < 0) - break; - if(i1 < 0 && i2 >= 0) { - nums1[i] = nums2[i2--]; - } - if(i1 >= 0 && i2 >= 0) { - if(nums1[i1] > nums2[i2]) { - nums1[i] = nums1[i1--]; - } else { - nums1[i] = nums2[i2--]; - } - } - } - } -}; \ No newline at end of file