FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc1.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc1.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (32 loc) · 990 Bytes
Breadcrumbs
leetcode
/
code
/
lc1.java
Copy path
File metadata and controls
36 lines (32 loc) · 990 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package
code
;
/*
* 1. Two Sum
* 题意:返回数组中和为给定数的下标
* 难度:Easy
* 分类:Array, HashTable
* 算法:题目说明了数组中一定有解,且解唯一,所以用哈希表记录已遍历的元素即可
*/
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
public
class
lc1
{
public
static
void
main
(
String
[]
args
) {
int
[]
nums
= {
2
,
7
,
11
,
15
};
int
target
=
9
;
int
[]
r
=
twoSum
(
nums
,
target
);
System
.
out
.
println
(
r
[
0
]);
System
.
out
.
println
(
r
[
1
]);
}
public
static
int
[]
twoSum
(
int
[]
nums
,
int
target
) {
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<
Integer
,
Integer
>();
int
[]
result
=
new
int
[
2
];
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++) {
if
(
map
.
containsKey
(
nums
[
i
])){
result
[
0
] =
map
.
get
(
nums
[
i
]);
result
[
1
] =
i
;
return
result
;
}
map
.
put
(
target
-
nums
[
i
],
i
);
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL