摘要: ``` 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) 编辑