Skip to content

Commit e5efe9a

Browse files
committed
Merge branch 'master' of https://github.com/KongJHong/leetcode
2 parents 5b59a90 + d7f4c7c commit e5efe9a

File tree

27 files changed

+795
-81
lines changed

27 files changed

+795
-81
lines changed

README.md

Lines changed: 80 additions & 80 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int myAtoi(String str) {
3+
int len = str.length();
4+
if (len == 0) return 0;
5+
char[] cs = str.toCharArray();
6+
int i = 0;
7+
while (i < len && cs[i] == ' ') i++;
8+
if (i==len) return 0;
9+
char c1 = cs[i];
10+
int sig = 1;
11+
if ((c1 > '9' || c1 < '0')) {
12+
if (c1 == '-') {
13+
sig = -1;
14+
i++;
15+
} else if (c1 == '+') {
16+
i++;
17+
} else return 0;
18+
}
19+
long v = 0,sv = 0;
20+
for (; i < len; i++) {
21+
char c = cs[i];
22+
if (c < '0' || c > '9') break;
23+
v = v * 10 + (c - '0');
24+
sv = v * sig;
25+
if (sv > Integer.MAX_VALUE) return Integer.MAX_VALUE;
26+
else if (sv < Integer.MIN_VALUE) return Integer.MIN_VALUE;
27+
}
28+
return (int) sv;
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const myAtoi = function(str){
2+
str = str.trim();
3+
if(!str) return 0;
4+
let isPositive = 1;
5+
let i = 0, ans = 0;
6+
if(str[i] === '+'){
7+
isPositive = 1;
8+
i++;
9+
}else if(str[i] === '-'){
10+
isPositive = 0;
11+
i++;
12+
}
13+
for(; i < str.length; i++){
14+
let t = str.charCodeAt(i) - 48;
15+
if(t > 9 || t < 0) break;
16+
if(ans > 2147483647/10 || ans > (2147483647-t)/10){
17+
return isPositive ? 2147483647 : -2147483648;
18+
}else{
19+
ans = ans*10 + t;
20+
}
21+
}
22+
return isPositive? ans : -ans;
23+
}

solution/0012.Integer to Roman/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class Solution {
66
public String intToRoman(int num) {
77
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
88
}
9-
}
9+

solution/0015.3Sum/Solution.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
class Solution {
2+
public:
3+
vector<vector<int> > threeSum(vector<int> &num) {
4+
return threeSumGen(num, 0);
5+
}
6+
7+
vector<vector<int> > threeSumGen(vector<int> &num, int target) {
8+
vector<vector<int>> allSol;
9+
if(num.size()<3) return allSol;
10+
sort(num.begin(),num.end());
11+
12+
for(int i=0; i<num.size()-2; i++) {
13+
if(i>0 && num[i]==num[i-1]) continue; // 去重
14+
15+
int left=i+1, right=num.size()-1;
16+
17+
while(left<right) {
18+
int curSum = num[left]+num[right];
19+
int curTarget = target-num[i];
20+
if(curSum==curTarget) {
21+
vector<int> sol;
22+
sol.push_back(num[i]);
23+
sol.push_back(num[left]);
24+
sol.push_back(num[right]);
25+
allSol.push_back(sol);
26+
left++;
27+
right--;
28+
while(num[left]==num[left-1]) left++;
29+
while(num[right]==num[right+1]) right--;
30+
}
31+
else if(curSum<curTarget)
32+
left++;
33+
else
34+
right--;
35+
}
36+
}
37+
return allSol;
38+
}
39+
40+
41+
42+
//backtrace TLE
43+
44+
// vector<vector<int>> threeSum(vector<int>& nums) {
45+
// sort(nums.begin(), nums.end());
46+
// if (nums.size() <= 2 || nums[0] > 0) return{};
47+
// set<vector<int>> res;
48+
// for (int i = 0; i < nums.size(); i++)
49+
// {
50+
// int target = 0 - nums[i];
51+
// int m = 0, n = nums.size() - 1;
52+
// while (m < n)
53+
// {
54+
// while (true)
55+
// {
56+
// if (m >= n) break;
57+
// if (m == i)
58+
// {
59+
// m++;
60+
// continue;
61+
// }
62+
// if (n == i)
63+
// {
64+
// n--;
65+
// continue;
66+
// }
67+
// if (nums[m] + nums[n] == target) {
68+
// vector<int> s = { nums[m++], nums[i], nums[n] };
69+
// sort(s.begin(), s.end());
70+
// res.insert(s);
71+
// break;
72+
// }
73+
// if (nums[m] + nums[n] > target) n--;
74+
// if (nums[m] + nums[n] < target) m++;
75+
// }
76+
// }
77+
78+
// }
79+
// vector<vector<int>> fres(res.begin(), res.end());
80+
// return fres;
81+
// }
82+
};
83+
84+
// accelerate the cin process.
85+
int x = []() {
86+
std::ios::sync_with_stdio(false);
87+
cin.tie(NULL);
88+
return 0;
89+
}();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const removeNthFromEnd = function(head, n){
2+
let left, before, right = head;
3+
left = before = {next: head};
4+
while(n--){
5+
right = right.next;
6+
}
7+
while(right){
8+
right =right.next;
9+
left = left.next;
10+
}
11+
left.next = left.next.next;
12+
return before.next;
13+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const mergeTwoLists2 = function (l1, l2) {
2+
if (l1 === null && l2 === null) {
3+
return null;
4+
}
5+
if (l1 !== null && l2 === null) {
6+
return l1;
7+
}
8+
if (l1 === null && l2 !== null) {
9+
return l2;
10+
}
11+
if (l1 !== null && l2 !== null) {
12+
let t = null, h = null;
13+
if (l1.val > l2.val) {
14+
t = l2;
15+
h = l2;
16+
l2 = l2.next;
17+
} else {
18+
t = l1;
19+
h = l1;
20+
l1 = l1.next;
21+
}
22+
while (l1 !== null && l2 !== null) {
23+
if (l1.val > l2.val) {
24+
t.next = l2;
25+
t = t.next;
26+
l2 = l2.next;
27+
} else {
28+
t.next = l1;
29+
t = t.next;
30+
l1 = l1.next;
31+
}
32+
}
33+
while (l1 !== null) {
34+
t.next = l1;
35+
l1 = l1.next;
36+
t = t.next;
37+
}
38+
while (l2 !== null) {
39+
t.next = l2;
40+
l2 = l2.next;
41+
t = t.next;
42+
}
43+
return h;
44+
}
45+
}
46+
47+
const mergeTwoLists = function (l1, l2) {
48+
if (l1 === null) return l2;
49+
if (l2 === null) return l1;
50+
if (l1.val < l2.val) {
51+
l1.next = mergeTwoLists(l1.next, l2);
52+
return l1;
53+
} else {
54+
l2.next = mergeTwoLists(l1, l2.next);
55+
return l2;
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
List<String> result = new ArrayList<>();
3+
public List<String> generateParenthesis(int n) {
4+
generateParenthesis(n, n, new char[n * 2], 0);
5+
return result;
6+
}
7+
void generateParenthesis(int left, int right, char[] cache, int index) {
8+
if (right == 0) {
9+
result.add(String.valueOf(cache));
10+
return;
11+
}
12+
if (left > 0) {
13+
cache[index] = '(';
14+
generateParenthesis(left - 1, right, cache, index + 1);
15+
}
16+
if (right > left) {
17+
cache[index] = ')';
18+
generateParenthesis(left, right - 1, cache, index + 1);
19+
}
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const removeDuplicates = function(nums){
2+
let i = 1;
3+
let t = 0;
4+
let t2 = nums[0];
5+
let res = 1;
6+
let length = nums.length;
7+
for(let k = 1; k < length; k++){
8+
if(nums[k] - nums[k-1] !== 0 && nums[k] !== t2){
9+
t2 = nums[k];
10+
t = nums[i];
11+
nums[i] = nums[k];
12+
nums[k] = t;
13+
i++;
14+
res++;
15+
}
16+
}
17+
return res;
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def longestValidParentheses(self, s):
3+
"""
4+
:type s: string
5+
:rtype int
6+
"""
7+
8+
Longest = temp = 0
9+
stack = []
10+
11+
for i in s:
12+
if i == '(' :
13+
stack.append(i)
14+
elif len(stack) != 0 and stack[-1] == '(' :
15+
stack.pop()
16+
temp += 2
17+
else:
18+
stack=[]
19+
if temp > Longest:
20+
Longest = temp
21+
temp = 0
22+
if temp > Longest:
23+
Longest = temp
24+
return Longest

0 commit comments

Comments
 (0)