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

Commit8bce341

Browse files
add 3237
1 parentfa257a9 commit8bce341

File tree

3 files changed

+101
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+101
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3237|[Alt and Tab Simulation](https://leetcode.com/problems/alt-and-tab-simulation/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java)|| Medium|
34
| 3234 |[Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) |[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java) | | Medium | Sliding Window
45
| 3233 |[Find the Count of Numbers Which Are Not Special](https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/) |[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java) | | Medium | Math
56
| 3232|[Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java)|| Easy|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
packagecom.fishercoder.solutions.fourththousand;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclass_3237 {
7+
publicstaticclassSolution1 {
8+
/**
9+
* My completely original solution, very natural to think of doubly linked list + hashmap.
10+
*/
11+
publicint[]simulationResult(int[]windows,int[]queries) {
12+
Map<Integer,DoublyLinkedListNode>map =newHashMap<>();
13+
DoublyLinkedListNodepre =buildList(windows,map);
14+
for (intq :queries) {
15+
moveToHead(q,pre,map);
16+
}
17+
returnbackToArray(pre,windows.length);
18+
}
19+
20+
privateint[]backToArray(DoublyLinkedListNodepre,intlength) {
21+
DoublyLinkedListNodetmp =pre;
22+
int[]ans =newint[length];
23+
for (inti =0;i <length;i++) {
24+
ans[i] =tmp.next.val;
25+
tmp =tmp.next;
26+
}
27+
returnans;
28+
}
29+
30+
privatevoidmoveToHead(intq,DoublyLinkedListNodeheadPrev,Map<Integer,DoublyLinkedListNode>map) {
31+
DoublyLinkedListNodenode =map.get(q);
32+
//if this window is already at the head, then we don't need to do anything
33+
if (headPrev.next ==node) {
34+
return;
35+
}
36+
//get this node's next and prev pointers
37+
DoublyLinkedListNodenext =node.next;
38+
DoublyLinkedListNodeprev =node.prev;
39+
//connect it's next to its previous' next, essentially cutting the current node out of the chain
40+
prev.next =next;
41+
//in case this is tail, we don't need to re-assign its next pointer
42+
if (next !=null) {
43+
next.prev =prev;
44+
}
45+
DoublyLinkedListNodeoldHead =headPrev.next;
46+
headPrev.next =node;
47+
node.next =oldHead;
48+
oldHead.prev =node;
49+
}
50+
51+
privateDoublyLinkedListNodebuildList(int[]windows,Map<Integer,DoublyLinkedListNode>map) {
52+
DoublyLinkedListNodepre =newDoublyLinkedListNode(-1);
53+
DoublyLinkedListNodetmp =pre;
54+
for (inti =0;i <windows.length;i++) {
55+
DoublyLinkedListNodenext =newDoublyLinkedListNode(windows[i]);
56+
next.prev =tmp;
57+
tmp.next =next;
58+
map.put(windows[i],next);
59+
tmp =tmp.next;
60+
}
61+
returnpre;
62+
}
63+
64+
publicstaticclassDoublyLinkedListNode {
65+
DoublyLinkedListNodeprev;
66+
DoublyLinkedListNodenext;
67+
intval;
68+
69+
publicDoublyLinkedListNode(intval) {
70+
this.val =val;
71+
}
72+
}
73+
}
74+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder.fourththousand;
2+
3+
importcom.fishercoder.solutions.fourththousand._3237;
4+
importorg.junit.jupiter.api.BeforeEach;
5+
importorg.junit.jupiter.api.Test;
6+
7+
importstaticorg.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
publicclass_3237Test {
10+
privatestatic_3237.Solution1solution1;
11+
12+
@BeforeEach
13+
publicvoidsetup() {
14+
solution1 =new_3237.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertArrayEquals(newint[]{2,3,1},solution1.simulationResult(newint[]{1,2,3},newint[]{3,3,2}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertArrayEquals(newint[]{3,1,4,2},solution1.simulationResult(newint[]{1,4,2,3},newint[]{4,1,3}));
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp