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

Commitcf2929c

Browse files
author
zhenyu zhang
committed
update Solution 138、881
1 parent3aa011a commitcf2929c

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

‎.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎src/com/hadley/_138.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.01.12
5+
Copy List with Random Pointer
6+
7+
*/
8+
9+
importjava.util.HashMap;
10+
11+
publicclass_138 {
12+
13+
classNode {
14+
intval;
15+
Nodenext;
16+
Noderandom;
17+
18+
publicNode(intval) {
19+
this.val =val;
20+
this.next =null;
21+
this.random =null;
22+
}
23+
}
24+
25+
publicNodecopyRandomList(Nodehead) {
26+
if(head ==null)returnnull;
27+
28+
HashMap<Node,Node>map =newHashMap<>();
29+
NodenewHead =newNode(head.val);
30+
map.put(head,newHead);
31+
32+
NodeoldP =head.next;
33+
NodenewP =newHead;
34+
while(oldP !=null){
35+
NodenewNode =newNode(oldP.val);
36+
map.put(oldP,newNode);
37+
newP.next =newNode;
38+
39+
oldP =oldP.next;
40+
newP =newP.next;
41+
42+
}
43+
44+
oldP =head;
45+
newP =newHead;
46+
47+
while(oldP !=null){
48+
newP.random =map.get(oldP.random);
49+
oldP =oldP.next;
50+
newP =newP.next;
51+
52+
}
53+
54+
returnnewHead;
55+
}
56+
}

‎src/com/hadley/_881.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2021.01.13
5+
Boats to Save People
6+
The i-th person has weight people[i], and each boat can carry a maximum weight of limit.
7+
8+
Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.
9+
10+
Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)
11+
12+
13+
14+
Example 1:
15+
Input: people = [1,2], limit = 3
16+
Output: 1
17+
Explanation: 1 boat (1, 2)
18+
19+
Example 2:
20+
Input: people = [3,2,2,1], limit = 3
21+
Output: 3
22+
Explanation: 3 boats (1, 2), (2) and (3)
23+
24+
Example 3:
25+
Input: people = [3,5,3,4], limit = 5
26+
Output: 4
27+
Explanation: 4 boats (3), (3), (4), (5)
28+
*/
29+
30+
publicclass_881 {
31+
32+
publicintnumRescueBoats(int[]people,intlimit) {
33+
34+
35+
return0;
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp