Skip to content

Commit 8acf88d

Browse files
author
wuyinjun
committed
update
1 parent 5a833f4 commit 8acf88d

File tree

11 files changed

+179
-21
lines changed

11 files changed

+179
-21
lines changed

1. 两数之和/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
class Solution:
4+
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
if len(nums) == 0:
6+
return []
7+
if len(nums) == 1:
8+
if nums[0] == target:
9+
return [0]
10+
else:
11+
return []
12+
if len(nums) == 2:
13+
if nums[0] + nums[1] == target:
14+
return [0, 1]
15+
else:
16+
return []
17+
for i in range(0, len(nums)):
18+
for j in range(i + 1, len(nums)):
19+
if nums[i] + nums[j] == target:
20+
return [i, j]
21+
return []
22+
23+
solution = Solution()
24+
result = solution.twoSum([3,2,4], 6)
25+
print(result)

1. 两数之和/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
struct Solution {
2+
3+
}
4+
5+
use std::collections::HashMap;
6+
7+
impl Solution {
8+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
9+
let mut hash: HashMap<i32, i32> = HashMap::new();
10+
let length = nums.len();
11+
12+
for i in 0..length {
13+
let rest = nums[i];
14+
if hash.contains_key(&rest) {
15+
let index = hash[&rest];
16+
return vec![index, i as i32];
17+
} else {
18+
hash.insert(target - rest, i as i32);
19+
}
20+
}
21+
return vec![];
22+
}
23+
}
24+
25+
fn main() {
26+
let result = Solution::two_sum(vec![3,3], 6);
27+
println!("{:?}", result);
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(105__)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(105__
7+
main.cpp)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
5+
struct TreeNode {
6+
int val;
7+
TreeNode *left;
8+
TreeNode *right;
9+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
10+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12+
};
13+
14+
class Solution {
15+
public:
16+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
17+
18+
}
19+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(cmake_testapp)
4+
5+
set(CMAKE_CXX_STANDARD 14)
6+
7+
add_executable(cmake_testapp main.cpp)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
5+
struct TreeNode {
6+
int val;
7+
TreeNode *left;
8+
TreeNode *right;
9+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
10+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
12+
};
13+
14+
class Solution {
15+
public:
16+
int ans = INT_MIN;
17+
18+
int maxPathSum(TreeNode* root) {
19+
return oneSideMax(root);
20+
}
21+
22+
int oneSideMax(TreeNode* root) {
23+
if (root == nullptr) return 0;
24+
25+
int left = max(0, oneSideMax(root->left));
26+
int right = max(0, oneSideMax(root->right));
27+
28+
ans = max(ans, left + right + root->val);
29+
return max(left, right) + root->val;
30+
}
31+
};
32+
33+
int main() {
34+
return 0;
35+
}

21. 合并两个有序链表/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution {
2+
3+
}
4+
5+
impl Solution {
6+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
7+
if nums.len() < 2 {
8+
return nums.len() as i32
9+
}
10+
11+
let mut left: usize = 0;
12+
let mut right: usize = 1;
13+
while right < nums.len() {
14+
if nums[left] == nums[right] {
15+
right += 1;
16+
} else {
17+
nums[left + 1] = nums[right];
18+
left += 1;
19+
right += 1;
20+
}
21+
}
22+
return left as i32 + 1;
23+
}
24+
}
25+
26+
fn main() {
27+
let mut list = vec![1,1,2];
28+
let result = Solution::remove_duplicates(&mut list);
29+
println!("{:?}", result);
30+
}

3. 无重复字符的最长子串/MyPlayground.playground/Contents.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ class Solution {
1919
return maxStr.count
2020
}
2121

22-
func lengthOfLongestSubstring(_ s: String) -> Int {
23-
var left = 0
24-
var right = 0
25-
var length = 0
26-
27-
var set = Set<Character>()
28-
29-
while right < s.count {
30-
let rightC = s[s.index(s.startIndex, offsetBy: right)]
31-
if !set.contains(rightC) {
32-
set.insert(rightC)
33-
length = max(right - left + 1, length)
34-
right += 1
35-
} else {
36-
let leftC = s[s.index(s.startIndex, offsetBy: left)]
37-
set.remove(leftC)
38-
left += 1
39-
}
40-
}
41-
return length
42-
}
22+
// func lengthOfLongestSubstring(_ s: String) -> Int {
23+
// var left = 0
24+
// var right = 0
25+
// var length = 0
26+
//
27+
// var set = Set<Character>()
28+
//
29+
// while right < s.count {
30+
// let rightC = s[s.index(s.startIndex, offsetBy: right)]
31+
// if !set.contains(rightC) {
32+
// set.insert(rightC)
33+
// length = max(right - left + 1, length)
34+
// right += 1
35+
// } else {
36+
// let leftC = s[s.index(s.startIndex, offsetBy: left)]
37+
// set.remove(leftC)
38+
// left += 1
39+
// }
40+
// }
41+
// return length
42+
// }
4343
}
4444

4545
let s = Solution()

0 commit comments

Comments
 (0)