diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 6a5d33a9..48412ed4 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -61,6 +61,8 @@ public: vector> threeSum(vector& nums) { vector> result; sort(nums.begin(), nums.end()); + // 找出a + b + c = 0 + // a = nums[i], b = nums[left], c = nums[right] for (int i = 0; i < nums.size(); i++) { // 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了 if (nums[i] > 0) { diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 425cb805..d896abc1 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -1,4 +1,3 @@ - ## 题目地址 https://leetcode-cn.com/problems/valid-anagram/ diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md new file mode 100644 index 00000000..fc0a779e --- /dev/null +++ b/problems/0344.反转字符串.md @@ -0,0 +1,19 @@ +## 题目地址 +https://leetcode-cn.com/problems/reverse-string/ + +## 思路 + +遍历数组的前一半,同时和后一半做交换就可以了 + +## 代码 + +``` +class Solution { +public: + void reverseString(vector& s) { + for (int i = 0, j = s.size() - 1; i < s.size()/2; i++, j--) { + swap(s[i],s[j]); + } + } +}; +```