Skip to content

Commit ba475e5

Browse files
committedMar 7, 2019
solve #1
1 parent 1c46c5a commit ba475e5

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed
 

Diff for: ‎README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44

55
项目结构以及template参考了[aylei](https://github.com/aylei)[leetcode-rust](https://github.com/aylei/leetcode-rust)项目,感谢大佬
66

7-
# 其他环境
7+
# 环境
88

99
* System: macOS
1010
* IDE:PyCharm(with Rust Plugin)
1111
* Python: 3.7
1212

13+
# 使用
14+
15+
* PyCharm会自动建立该项目的Python venv环境
16+
* `pip install -r requirements.txt` 安装所需的py组件
17+
* `python new.py {id}` 生成对应题目的模板
18+
* `cargo test test_{id}` 运行测试样例
19+
1320
# 目录
1421

22+
* [1 - Two Sum](./src/p0001_two_sum.rs)

Diff for: ‎new.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def new_code_file(pid, problem):
106106
assert len(rust_code) == 1, f'problem #{pid} has no rust support yet'
107107
rust_code = rust_code[0]['defaultCode']
108108
# 判断文件是否已经存在
109-
file_name = 'p{:04d}_{}'.format(pid, problem.title_slug)
109+
file_name = 'p{:04d}_{}'.format(pid, problem.title_slug).replace('-', '_')
110110
file_path = f'./src/{file_name}.rs'
111111
assert not os.path.exists(file_path), f'problem #{pid} already initialized'
112112
# 按模板生成代码

Diff for: ‎src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#[macro_use]
22
pub mod utils;
33

4+
mod p0001_two_sum;

Diff for: ‎src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
fn main() {
2-
println!("Hello, world!");
32
}

Diff for: ‎src/p0001_two_sum.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* [1] Two Sum
3+
*
4+
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
5+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
* Example:
7+
*
8+
* Given nums = [2, 7, 11, 15], target = 9,
9+
*
10+
* Because nums[0] + nums[1] = 2 + 7 = 9,
11+
* return [0, 1].
12+
*  
13+
*
14+
*/
15+
16+
pub struct Solution {}
17+
18+
// submission codes start here
19+
20+
use std::collections::HashMap;
21+
impl Solution {
22+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
23+
let mut m = HashMap::new();
24+
for (i, x) in nums.iter().enumerate() {
25+
if m.get(&(target - x)) == None {
26+
m.insert(x, i);
27+
} else {
28+
return vec![*m.get(&(target - x)).unwrap() as i32, i as i32];
29+
}
30+
}
31+
return vec![-1, -1];
32+
}
33+
}
34+
35+
// submission codes end
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test_1() {
43+
assert_eq!(vec![0, 1], Solution::two_sum(vec![2, 7, 11, 15], 9));
44+
assert_eq!(vec![1, 2], Solution::two_sum(vec![3, 2, 4], 6));
45+
}
46+
}

Diff for: ‎template.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* __PROBLEM_DESC__
55
*/
6+
67
pub struct Solution {}__EXTRA_USE__
78

89
// submission codes start here

0 commit comments

Comments
 (0)
Please sign in to comment.