摘要: class Solution { public: int searchInsert(vector& nums, int target) { if (nums.back() 阅读全文
posted @ 2019-04-08 21:07 JohnRed 阅读(68) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: vector searchRange(vector& nums, int target) { vector res(2, 1); int left = 0, right = nums.size() 1; while (left 阅读全文
posted @ 2019-04-08 21:05 JohnRed 阅读(99) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int search(vector& nums, int target) { int left = 0, right = nums.size() - 1; while (left = target) left = mid + 1; else right = mid - ... 阅读全文
posted @ 2019-04-08 20:58 JohnRed 阅读(66) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int longestValidParentheses(string s) { int result=0; s=')'+s; vector dp(s.length(),0); for(int i=1;i 阅读全文
posted @ 2019-04-08 20:56 JohnRed 阅读(94) 评论(0) 推荐(0) 编辑
摘要: class Solution { public: void nextPermutation(vector& nums) {int n = nums.size(), i = n 2, j = n 1; while (i = 0 && nums[i] = nums[i + 1]) i; if (i = 阅读全文
posted @ 2019-04-08 20:52 JohnRed 阅读(70) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: vector findSubstring(string s, vector& words) { vector res; if (s.empty() || words.empty()) return res; int n = words.size(), m = words[0].size... 阅读全文
posted @ 2019-04-08 20:50 JohnRed 阅读(71) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int divide(int dividend, int divisor) { if (divisor == 0 || (dividend == INT_MIN && divisor == 1)) return INT_MAX; long l 阅读全文
posted @ 2019-04-08 19:57 JohnRed 阅读(57) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int removeElement(vector& nums, int val) { int res = 0; for (int i = 0; i 阅读全文
posted @ 2019-04-08 19:53 JohnRed 阅读(55) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int removeDuplicates(vector& nums) { if (nums.empty()) return 0; int pre = 0, cur = 0, n = nums.size(); while (cur 阅读全文
posted @ 2019-04-08 19:52 JohnRed 阅读(59) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { if (!head || k == 1) return head; ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = head; ... 阅读全文
posted @ 2019-04-08 19:51 JohnRed 阅读(66) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: ListNode* swapPairs(ListNode* head) { ListNode *dummy = new ListNode(-1), *pre = dummy; dummy->next = head; while (pre->next && pre->next->next... 阅读全文
posted @ 2019-04-08 19:48 JohnRed 阅读(80) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: ListNode* mergeKLists(vector& lists) { if (lists.empty()) return NULL; int n = lists.size(); while (n > 1) { int k = (n + 1) / 2; ... 阅读全文
posted @ 2019-04-08 19:43 JohnRed 阅读(61) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: vector generateParenthesis(int n) { vector res; generateParenthesisDFS(n, n, "", res); return res; } void generateParenthesisDFS(int le... 阅读全文
posted @ 2019-04-08 19:38 JohnRed 阅读(71) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *dummy = new ListNode(-1), *cur = dummy; while (l1 && l2) { if (l1->val val)... 阅读全文
posted @ 2019-04-08 19:37 JohnRed 阅读(64) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: bool isValid(string s) { stack parentheses; for (int i = 0; i 阅读全文
posted @ 2019-04-08 19:35 JohnRed 阅读(77) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (!head->next) return NULL; ListNode *pre = head, *cur = head; for (int i = 0; i next; ... 阅读全文
posted @ 2019-04-08 19:34 JohnRed 阅读(84) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: vector> fourSum(vector &nums, int target) { set> res; sort(nums.begin(), nums.end()); for (int i = 0; i i + 1 && nums[j] == nums[j - 1]) conti... 阅读全文
posted @ 2019-04-08 19:33 JohnRed 阅读(109) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: int threeSumClosest(vector& nums, int target) { int closest = nums[0] + nums[1] + nums[2]; int diff = abs(closest target) 阅读全文
posted @ 2019-04-08 19:32 JohnRed 阅读(73) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: vector> threeSum(vector& nums) { set> res; sort(nums.begin(), nums.end()); if (nums.empty() || nums.back() 0) return {}; for (int k = ... 阅读全文
posted @ 2019-04-08 18:20 JohnRed 阅读(66) 评论(0) 推荐(0) 编辑
摘要: ``` class Solution { public: string longestCommonPrefix(vector& strs) { if (strs.empty()) return ""; string res = ""; for (int j = 0; j = strs[i].size() || strs[i][j] != c)... 阅读全文
posted @ 2019-04-08 18:17 JohnRed 阅读(57) 评论(0) 推荐(0) 编辑