Skip to content

Commit 18577b3

Browse files
author
xwk1246
committed
push all
1 parent 1bb4323 commit 18577b3

12 files changed

+355
-12
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*
2-
!*.*
2+
!*.cpp
33
!*/

1.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
#include <vector>
22
#include <unordered_map>
3-
#include <unordered_set>
4-
#include <cmath>
53
using namespace std;
64

75
vector<int> twoSum(vector<int>& nums, int target) {
8-
unordered_map<int, int>record;
9-
for (int i = 0; i < nums.size(); i++) {
10-
unordered_map<int, int>::iterator result;
11-
if ((result = record.find(target - nums[i])) != record.end()) {
12-
return { i, result->second };
13-
}
14-
record.insert(pair<int, int>(nums[i], i));
15-
}
16-
return {};
6+
177
}

1.two-sum.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=1 lang=cpp
3+
*
4+
* [1] Two Sum
5+
*/
6+
7+
// @lc code=start
8+
#include <unordered_map>
9+
#include <vector>
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
vector<int> twoSum(vector<int>& nums, int target) {
15+
unordered_map<int, int> record;
16+
int index = 0;
17+
18+
for (int num : nums) {
19+
auto tmp = record.find(target - num);
20+
if (tmp != record.end()) {
21+
return { index , tmp->second };
22+
}
23+
record.insert(pair<int, int>(num, index));
24+
index++;
25+
}
26+
return {};
27+
}
28+
};
29+
// @lc code=end
30+
31+
int main() {
32+
vector<int> nums = { 3, 2, 4 };
33+
int target = 6;
34+
Solution S1;
35+
S1.twoSum(nums, target);
36+
}

15.3-sum.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode id=15 lang=cpp
3+
*
4+
* [15] 3Sum
5+
*/
6+
7+
// @lc code=start
8+
#include <vector>
9+
#include <algorithm>
10+
using namespace std;
11+
12+
class Solution {
13+
public:
14+
vector<vector<int>> threeSum(vector<int>& nums) {
15+
sort(nums.begin(), nums.end());
16+
vector<vector<int>> result;
17+
int right;
18+
int left;
19+
for (int i = 0; i < nums.size(); i++) {
20+
if (i > 0 && nums[i - 1] == nums[i])continue;
21+
right = nums.size() - 1;
22+
left = i + 1;
23+
while (left < right) {
24+
if (nums[i] + nums[right] + nums[left] < 0) {
25+
left++;
26+
}
27+
else if (nums[i] + nums[right] + nums[left] > 0) {
28+
right--;
29+
}
30+
else {
31+
result.push_back({ nums[i], nums[left], nums[right] });
32+
while (left < right && nums[left] == nums[left + 1])left++;
33+
while (left < right && nums[right - 1] == nums[right])right--;
34+
left++;
35+
right--;
36+
}
37+
}
38+
39+
}
40+
return result;
41+
}
42+
};
43+
// @lc code=end
44+
int main() {
45+
Solution s;
46+
vector<int> nums = { -1,0,1 };
47+
s.threeSum(nums);
48+
return 0;
49+
}
50+

151.reverse-words-in-a-string.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
/*
7+
* @lc app=leetcode id=151 lang=cpp
8+
*
9+
* [151] Reverse Words in a String
10+
*/
11+
12+
// @lc code=start
13+
class Solution {
14+
public:
15+
string reverseWords(string s) {
16+
int slow = 0, fast = 0;
17+
reverse(s.begin(), s.end());
18+
while (fast < s.size()) {
19+
if (fast == 0 && s[fast] == ' ' || (s[fast] == ' ' && s[fast - 1] == ' ')) {
20+
fast++;
21+
continue;
22+
}
23+
s[slow] = s[fast];
24+
slow++;
25+
fast++;
26+
}
27+
s.resize(slow);
28+
while (s.back() == ' ') {
29+
s.pop_back();
30+
}
31+
for (slow = 0, fast = 0; fast < (int)s.size(); ) {
32+
while (s[fast] != ' ' && fast < s.size()) {
33+
fast++;
34+
};
35+
reverse(s.begin() + slow, s.begin() + fast);
36+
fast++;
37+
slow = fast;
38+
}
39+
return s;
40+
}
41+
42+
};
43+
// @lc code=end
44+
45+
int main() {
46+
Solution S1;
47+
cout << S1.reverseWords(" a ");
48+
return 0;
49+
50+
}

18.4-sum.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <vector>
2+
#include <iostream>
3+
#include <algorithm>
4+
using namespace std;
5+
/*
6+
* @lc app=leetcode id=18 lang=cpp
7+
*
8+
* [18] 4Sum
9+
*/
10+
11+
// -2,-1,0,0,1,2
12+
// @lc code=start
13+
class Solution {
14+
public:
15+
vector<vector<int>> fourSum(vector<int>& nums, int target) {
16+
sort(nums.begin(), nums.end());
17+
vector<vector<int>>result;
18+
for (int i = 0; i < nums.size(); i++) {
19+
while (i > 0 && i < nums.size() && nums[i] == nums[i - 1])i++;
20+
for (int j = i + 1; j < nums.size(); j++) {
21+
while (j > i + 1 && j < nums.size() && nums[j] == nums[j - 1])j++;
22+
int left, right;
23+
left = j + 1;
24+
right = nums.size() - 1;
25+
while (left < right) {
26+
long long sum = (long long)nums[i] + (long long)nums[j] + (long long)nums[left] + (long long)nums[right];
27+
if (sum > target)right--;
28+
else if (sum < target)left++;
29+
else {
30+
while (left < right && nums[left] == nums[left + 1])left++;
31+
while (left < right && nums[right] == nums[right - 1])right--;
32+
result.push_back({ nums[i], nums[j], nums[left], nums[right] });
33+
left++;
34+
right--;
35+
}
36+
}
37+
}
38+
}
39+
return result;
40+
41+
}
42+
};
43+
// @lc code=end
44+
int main() {
45+
Solution S1;
46+
vector<int>tmp = { 1000000000,1000000000,1000000000,1000000000 };
47+
S1.fourSum(tmp, 0);
48+
49+
}

28.implement-str-str.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <string>
2+
using namespace std;
3+
/*
4+
* @lc app=leetcode id=28 lang=cpp
5+
*
6+
* [28] Implement strStr()
7+
*/
8+
9+
// @lc code=start
10+
class Solution {
11+
public:
12+
int strStr(string haystack, string needle) {
13+
14+
}
15+
};
16+
// @lc code=end
17+

344.reverse-string.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vector>
2+
#include <algorithm>
3+
using namespace std;
4+
/*
5+
* @lc app=leetcode id=344 lang=cpp
6+
*
7+
* [344] Reverse String
8+
*/
9+
10+
// @lc code=start
11+
class Solution {
12+
public:
13+
void reverseString(vector<char>& s) {
14+
for (int i = 0; i < s.size() / 2;i++) {
15+
swap(s[i], s[s.size() - 1 - i]);
16+
}
17+
}
18+
19+
};
20+
// @lc code=end
21+
22+
int main() {
23+
Solution S1;
24+
vector<char>s = { 'h', 'e', 'l', 'l', 'o' };
25+
S1.reverseString(s);
26+
}
27+

383.ransom-note.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode id=383 lang=cpp
3+
*
4+
* [383] Ransom Note
5+
*/
6+
7+
// @lc code=start
8+
#include <string>
9+
#include <unordered_map>
10+
#include <vector>
11+
#include <iostream>
12+
13+
using namespace std;
14+
15+
16+
class Solution {
17+
public:
18+
bool canConstruct(string ransomNote, string magazine) {
19+
vector<int>record(26, 0);
20+
for (auto a : magazine) {
21+
record[a - 'a']++;
22+
}
23+
for (auto a : ransomNote) {
24+
if (record[a - 'a'] <= 0) {
25+
return false;
26+
}
27+
record[a - 'a']--;
28+
}
29+
return true;
30+
}
31+
};
32+
// @lc code=end
33+
34+
int main() {
35+
Solution s;
36+
cout << s.canConstruct("aa", "ab");
37+
}
38+

541.reverse-string-ii.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <vector>
2+
#include <string>
3+
#include <algorithm>
4+
#include <iostream>
5+
using namespace std;
6+
/*
7+
* @lc app=leetcode id=541 lang=cpp
8+
*
9+
* [541] Reverse String II
10+
*/
11+
12+
// @lc code=start
13+
class Solution {
14+
public:
15+
string reverseStr(string s, int k) {
16+
for (int i = 0; i < s.length(); i += 2 * k) {
17+
if ((s.end() - s.begin() - i) < k) {
18+
reverse(s.begin() + i, s.end());
19+
continue;
20+
}
21+
reverse(s.begin() + i, s.begin() + k + i);
22+
}
23+
return s;
24+
}
25+
};
26+
// @lc code=end
27+
int main() {
28+
Solution S1;
29+
cout << S1.reverseStr("a", 1);
30+
return 0;
31+
32+
}
33+

0 commit comments

Comments
 (0)