Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd197a23

Browse files
committed
feat: add leetcode-1 solution.
1 parent5383f85 commitd197a23

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
packagecom.yeahqing.easy._0001;
22

3+
importjava.util.*;
4+
35
/**
46
* @author YeahQing
57
* @date 2022/9/28
68
*/
7-
publicclassSolution {
9+
classSolution {
10+
publicint[]twoSum(int[]nums,inttarget) {
11+
HashMap<Integer,Integer>map =newHashMap<>();
12+
13+
for (inti =0;i <nums.length;i++) {
14+
if (map.containsKey(nums[i])) {
15+
returnnewint[]{i,map.get(nums[i])};
16+
}
17+
// 找target-nums[i]是否存在
18+
map.put(target-nums[i],i);
19+
}
20+
returnnewint[]{};
21+
}
22+
23+
publicstaticvoidmain(String[]args) {
24+
Solutionsolution =newSolution();
25+
Scannerscan =newScanner(System.in);
26+
String[]arr =scan.nextLine().split(",");
27+
int[]nums =newint[arr.length];
28+
for (inti =0;i <nums.length;i++) {
29+
nums[i] =Integer.parseInt(arr[i]);
30+
}
31+
inttarget =scan.nextInt();
32+
System.out.println(Arrays.toString(solution.twoSum(nums,target)));
33+
scan.close();
34+
}
835
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagecom.yeahqing.structure;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.List;
5+
6+
/**
7+
* @author YeahQing
8+
* @date 2022/9/28
9+
*/
10+
publicclassInterval {
11+
publicintstart;
12+
publicintend;
13+
14+
publicInterval() {
15+
start =0;
16+
end =0;
17+
}
18+
19+
publicInterval(ints,inte) {
20+
start =s;
21+
end =e;
22+
}
23+
24+
publicstaticList<Interval>createTestData(Stringdata) {
25+
List<Interval>list =newArrayList<>();
26+
String[]d =data.substring(1,data.length() -1).split("],\\[");
27+
for (Strings :d) {
28+
String[]sub =s.split(",");
29+
// parseInt返回int, valueOf返回Integer
30+
list.add(newInterval(Integer.parseInt(sub[0]),Integer.parseInt(sub[1])));
31+
}
32+
returnlist;
33+
}
34+
35+
publicstaticvoidprint(List<Interval>list) {
36+
if (list ==null) {
37+
System.out.println("null");
38+
return;
39+
}
40+
StringBuildersb =newStringBuilder();
41+
for (Intervalinterval :list) {
42+
sb.append("[")
43+
.append(interval.start)
44+
.append(",")
45+
.append(interval.end)
46+
.append("],");
47+
}
48+
System.out.println(sb.substring(0,sb.length() -1));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp