Skip to content

Commit 13658a4

Browse files
refactor 25
1 parent a62b5b9 commit 13658a4

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _If you like this project, please give me a star._ ★
44

55
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
66
7-
###If you feel benefited from Leetcode and loves it, please consider to [donate to Leetcode](https://leetcode.com/donate/) in order to help us build the best OJ platform.
7+
##If you feel benefited from Leetcode and loves it, please consider to [donate to Leetcode](https://leetcode.com/donate/) in order to help us build the best OJ platform.
88

99
## Contributing
1010
Your ideas/fixes/algorithms are more than welcome!
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.ListNode;
4-
import com.fishercoder.common.utils.CommonUtils;
54

65
/**
6+
* 25. Reverse Nodes in k-Group
77
* Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
8-
9-
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
10-
11-
You may not alter the values in the nodes, only nodes itself may be changed.
12-
13-
Only constant memory is allowed.
8+
* k is a positive integer and is less than or equal to the length of the linked list.
9+
* If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
10+
* You may not alter the values in the nodes, only nodes itself may be changed.
11+
* Only constant memory is allowed.
1412
1513
For example,
1614
Given this linked list: 1->2->3->4->5
@@ -19,8 +17,11 @@
1917
2018
For k = 3, you should return: 3->2->1->4->5
2119
*/
20+
2221
public class _25 {
2322

23+
/**We use recursion to go all the way until the end: when the number of nodes are smaller than k;
24+
* then we start to reverse each group of k nodes from the end towards the start.*/
2425
public ListNode reverseKGroup(ListNode head, int k) {
2526
ListNode curr = head;
2627
int count = 0;
@@ -31,31 +32,21 @@ public ListNode reverseKGroup(ListNode head, int k) {
3132
}
3233

3334
if (count == k) {
34-
//if k+1 is found
35-
curr = reverseKGroup(curr, k);//reverse list that has k+1 as head
35+
/**after this below recursive call finishes, it'll return head;
36+
* then this returned "head" will become "curr", while the head
37+
* in its previous callstack is the real head after this call.
38+
* Setting up a break point will make all of this crystal clear.*/
39+
curr = reverseKGroup(curr, k);
3640

37-
while (count > 0) {
41+
while (count-- > 0) {
3842
ListNode temp = head.next;
3943
head.next = curr;
4044
curr = head;
4145
head = temp;
42-
count--;
4346
}
4447
head = curr;
4548
}
4649
return head;//we run out of nodes before we hit count == k, so we'll just directly return head in this case as well
4750
}
4851

49-
public static void main(String... args) {
50-
ListNode head = new ListNode(1);
51-
head.next = new ListNode(2);
52-
head.next.next = new ListNode(3);
53-
head.next.next.next = new ListNode(4);
54-
head.next.next.next.next = new ListNode(5);
55-
56-
CommonUtils.printList(head);
57-
_25 test = new _25();
58-
CommonUtils.printList(test.reverseKGroup(head, 2));
59-
}
60-
61-
}
52+
}

src/main/java/com/fishercoder/solutions/_517.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@ public int findMinMoves(int[] machines) {
6060
}
6161
return max;
6262
}
63+
64+
public static class Solution2 {
65+
/**TODO: finish it.*/
66+
public int findMinMoves(int[] machines) {
67+
int totalDresses = 0;
68+
for (int i = 0; i < machines.length; i++) {
69+
totalDresses += machines[i];
70+
}
71+
if (totalDresses / machines.length == 0 || totalDresses % machines.length != 0) {
72+
return -1;
73+
}
74+
return -1;
75+
}
76+
}
6377
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.common.utils.LinkedListUtils;
5+
import com.fishercoder.solutions._25;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _25Test {
12+
private static _25 test;
13+
private static ListNode actual;
14+
private static ListNode expected;
15+
private static ListNode head;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
test = new _25();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
head = LinkedListUtils.contructLinkedList(new int[]{1, 2, 3, 4, 5});
25+
actual = test.reverseKGroup(head, 2);
26+
expected = LinkedListUtils.contructLinkedList(new int[]{2, 1, 4, 3, 5});
27+
assertEquals(actual, expected);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)