19 January 2020
LeetCode(86) -- 25, 73, 77
by Jerry Zhang
P25. Reverse Nodes in k-Group (Hard)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
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.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
Only constant extra memory is allowed.
You may not alter the values in the list’s nodes, only nodes itself may be changed.
My Solution
public class P25_Reverse_Nodes_in_k_Group {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode tail = findTail(head, k);
ListNode before = dummy;
while (tail != null) {
ListNode after = tail.next;
ListNode prev = before;
ListNode current = head;
ListNode next = current.next;
while (current != after) {
current.next = prev;
prev = current;
current = next;
if (next != null) {
next = next.next;
}
}
before.next = prev;
before = head;
head.next = after;
head = head.next;
tail = findTail(head, k);
}
return dummy.next;
}
private ListNode findTail(ListNode head, int k) {
if (head == null) {
return null;
}
int count = 1;
ListNode current = head;
while (current.next != null && count != k) {
current = current.next;
count++;
}
if (count == k) {
return current;
}
return null;
}
}
100%
The Best Solution
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode dum = dummy;
int i = 0;
while (head != null) {
i++;
if (i % k == 0) {
dum = reverse(dum, head.next);
head = dum.next;
} else {
head = head.next;
}
}
return dummy.next;
}
public ListNode reverse(ListNode lastTail, ListNode end) {
ListNode leadingHead = lastTail.next;
ListNode returnNode = leadingHead;
ListNode pre = lastTail;
while (leadingHead != end) { // just normal reversing, like "Reverse Linked list I"
ListNode then = leadingHead.next;
leadingHead.next = pre;
pre = leadingHead;
leadingHead = then;
}
lastTail.next = pre;
returnNode.next = end;
return returnNode;
}
}
P73. Set Matrix Zeroes (Medium)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
My Solution
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
}
100%
P77. Combinations (Medium)
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
My Solution
class Solution {
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
backtrack(nums, 0, new ArrayList<>(), k);
return ans;
}
private void backtrack(int[] nums, int start, List<Integer> tempList, int k) {
if (tempList.size() == k) {
ans.add(new ArrayList<>(tempList));
return;
}
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(nums, i + 1, tempList, k);
tempList.remove(tempList.size() - 1);
}
}
}
70%
The Best Solution
class Solution {
List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> combine(int n, int k){
int[] array = new int[n];
for(int i=1; i<=n; i++){
array[i-1] = i;
}
List<Integer> l = new ArrayList<>();
recursiveCombine(array, 0, k, l);
return list;
}
private void recursiveCombine(int[] array, int currCursor, int leftK, List<Integer> l) {
if(leftK==0){
list.add(new ArrayList<>(l));
return;
}
for(int i=currCursor; i<=array.length-leftK; i++){
l.add(array[i]);
recursiveCombine(array, i+1, leftK -1, l);
l.remove(l.size()-1);
}
}
}
进得越深,需要做的loop次数越少。
tags: LeetCode