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

Commit010064e

Browse files
single number I/II, remove Nth node from end of list
1 parentc5d5a52 commit010064e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
packageeasy;
2+
3+
importutils.CommonUtils;
4+
importclasses.ListNode;
5+
6+
/**19. Remove Nth Node From End of List QuestionEditorial Solution My Submissions
7+
Total Accepted: 122330
8+
Total Submissions: 400291
9+
Difficulty: Easy
10+
Given a linked list, remove the nth node from the end of list and return its head.
11+
12+
For example,
13+
14+
Given linked list: 1->2->3->4->5, and n = 2.
15+
16+
After removing the second node from the end, the linked list becomes 1->2->3->5.
17+
Note:
18+
Given n will always be valid.
19+
Try to do this in one pass.*/
20+
publicclassRemoveNthNodeFromEndOfList {
21+
publicListNoderemoveNthFromEnd(ListNodehead,intn) {
22+
ListNodetemp =head;
23+
intlen =0;
24+
while(temp !=null){
25+
temp =temp.next;
26+
len++;
27+
}
28+
if(n ==len)returnhead.next;
29+
30+
temp =head;
31+
intcut =len-n;
32+
while(cut-- >1){
33+
temp =temp.next;
34+
}
35+
if(temp.next !=null){
36+
temp.next =temp.next.next;
37+
returnhead;
38+
}
39+
returnnull;
40+
}
41+
42+
publicstaticvoidmain(String...strings){
43+
ListNodehead =newListNode(1);
44+
head.next =newListNode(2);
45+
RemoveNthNodeFromEndOfListtest =newRemoveNthNodeFromEndOfList();
46+
ListNoderes =test.removeNthFromEnd(head,2);
47+
CommonUtils.printList(res);
48+
}
49+
}

‎MEDIUM/src/medium/SingleNumberII.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagemedium;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
/**137. Single Number II QuestionEditorial Solution My Submissions
7+
Total Accepted: 91512
8+
Total Submissions: 236469
9+
Difficulty: Medium
10+
Given an array of integers, every element appears three times except for one. Find that single one.
11+
12+
Note:
13+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
14+
15+
Hide Tags Bit Manipulation
16+
Show Similar Problems
17+
*/
18+
publicclassSingleNumberII {
19+
20+
publicintsingleNumber(int[]nums) {
21+
Map<Integer,Integer>map =newHashMap();
22+
for(inti :nums){
23+
map.put(i,map.getOrDefault(i,0) +1);
24+
}
25+
for(intkey :map.keySet()){
26+
if(map.get(key) !=3)returnkey;
27+
}
28+
return0;
29+
}
30+
31+
publicstaticvoidmain(String...strings){
32+
int[]nums =newint[]{2,2,3,2};
33+
SingleNumberIItest =newSingleNumberII();
34+
System.out.println(test.singleNumber(nums));
35+
}
36+
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagemedium;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
/**260. Single Number III QuestionEditorial Solution My Submissions
7+
Total Accepted: 42536
8+
Total Submissions: 92175
9+
Difficulty: Medium
10+
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
11+
12+
For example:
13+
14+
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
15+
16+
Note:
17+
The order of the result is not important. So in the above example, [5, 3] is also correct.
18+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
19+
*/
20+
publicclassSingleNumberIII {
21+
//Approach 1: normal hashmap
22+
publicint[]singleNumber(int[]nums) {
23+
Map<Integer,Integer>map =newHashMap();
24+
for(inti :nums){
25+
map.put(i,map.getOrDefault(i,0)+1);
26+
}
27+
28+
int[]res =newint[2];
29+
intindex =0;
30+
for(intkey :map.keySet()){
31+
if(map.get(key) ==1)res[index++] =key;
32+
if(index ==2)break;
33+
}
34+
returnres;
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp