Skip to content

Commit 213c3b3

Browse files
committed
Add solution 001
0 parents  commit 213c3b3

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# LeetCode
2+
![LeetCode-GitHub](http://p9ucdlghd.bkt.clouddn.com/leetcode-github.png)
3+
4+
[![PRs Welcome](https://img.shields.io/badge/PRs-Welcome-brightgreen.svg)](http://makeapullrequest.com)
5+
[![Java Version](https://img.shields.io/badge/Lang-Java-red.svg)](https://github.com/yanglbme/qa)
6+
## Introduction
7+
Complete solutions to Leetcode problems, updated daily.
8+
9+
## Solution List
10+
11+
### Easy
12+
13+
| # | Title | Tags |
14+
|---|---|---|
15+
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
16+
17+
### Medium
18+
19+
| # | Title | Tags |
20+
|---|---|---|
21+
22+
### Hard
23+
24+
| # | Title | Tags |
25+
|---|---|---|
26+
27+
## Contributions
28+
I'm looking for long-term contributors/partners to this repo! Send me PRs if you're interested! See the following:
29+
- Fork this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
30+
- Submit a pull request with your changes!
31+
32+
## Contributors
33+
34+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
35+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center>[<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> |
36+
|---|---|---|---|---|---|---|
37+
38+
<!-- ALL-CONTRIBUTORS-LIST:END -->

solution/001.Two Sum/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## 两数之和
2+
### 题目描述
3+
4+
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
5+
6+
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
7+
8+
示例:
9+
```
10+
给定 nums = [2, 7, 11, 15], target = 9
11+
12+
因为 nums[0] + nums[1] = 2 + 7 = 9
13+
所以返回 [0, 1]
14+
```
15+
16+
### 解法
17+
利用 HashMap 记录数组元素值和对应的下标,对于一个数 nums[i],判断 target - nums[i] 是否存在 HashMap 中,存在的话,返回两个下标组成的数组。注意,已存在的元素下标在前,当前元素下标在后。
18+
19+
```java
20+
class Solution {
21+
public int[] twoSum(int[] nums, int target) {
22+
Map<Integer, Integer> map = new HashMap<>();
23+
for (int i = 0; i < nums.length; ++i) {
24+
if (map.containsKey(target - nums[i])) {
25+
return new int[] {map.get(target - nums[i]), i};
26+
}
27+
map.put(nums[i], i);
28+
}
29+
return null;
30+
}
31+
}
32+
```

solution/001.Two Sum/Solution.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i < nums.length; ++i) {
5+
if (map.containsKey(target - nums[i])) {
6+
return new int[] {map.get(target - nums[i]), i};
7+
}
8+
map.put(nums[i], i);
9+
}
10+
return null;
11+
}
12+
}

0 commit comments

Comments
 (0)