Java implementation of leetcode54, 59, 61 questions
54. Spiral Matrix
Title description
Given a matrix containing mxn elements (m rows, n columns), please follow the clockwise spiral order to return all the elements in the matrix.
Example 1:
Input:
[
[1, 2, 3 ],
[4, 5, 6 ],
[7, 8, 9]
]
Output: [1,2,3,6,9,8,7,4,5 ]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12 ,11,10,9,5,6,7]
Ideas
If you go to the right to the end, change to the bottom, if you go to the end, you change to the left, if you go to the end, you change to the top, and if you go to the end, you change to the right. Set the visited array to indicate if you have visited
Code
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> order = new ArrayList<Integer>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return order;
}
int rows = matrix.length, columns = matrix[0].length;
boolean[][] visited = new boolean[rows][columns];
int total = rows * columns;
int row = 0, column = 0;
int[][] directions = {
{
0, 1}, {
1, 0}, {
0, -1}, {
-1, 0}};
int directionIndex = 0;
for (int i = 0; i < total; i++) {
order.add(matrix[row][column]);
visited[row][column] = true;
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
directionIndex = (directionIndex + 1) % 4;
}
row += directions[directionIndex][0];
column += directions[directionIndex][1];
}
return order;
}
}
59. Spiral Matrix II
Title description
Given a positive integer n, generate a square matrix containing all elements from 1 to n2, and the elements are spirally arranged in clockwise order.
Example:
Input: 3
Output:
[
[1, 2, 3 ],
[8, 9, 4 ],
[7, 6, 5]
]
Ideas
Right, down, left and up, let the boundary change every time you reach the boundary, for example, when the line ends right to the bottom, let the number of lines ++
Code
class Solution {
public int[][] generateMatrix(int n) {
int l = 0, r = n - 1, t = 0, b = n - 1;
int[][] mat = new int[n][n];
int num = 1, tar = n * n;
while(num <= tar){
for(int i = l; i <= r; i++) mat[t][i] = num++; // left to right.
t++;
for(int i = t; i <= b; i++) mat[i][r] = num++; // top to bottom.
r--;
for(int i = r; i >= l; i--) mat[b][i] = num++; // right to left.
b--;
for(int i = b; i >= t; i--) mat[i][l] = num++; // bottom to top.
l++;
}
return mat;
}
}
61. Rotating linked list
Title description
Given a linked list, rotate the linked list and move each node of the linked list k positions to the right, where k is a non-negative number.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
Rotate to the right 1 step: 5- >1->2->3->4->NULL
Rotate right 2 steps: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
Rotate one step to the right: 2->0->1->NULL
direction Rotate right 2 steps: 1->2->0->NULL
Rotate right 3 steps: 0->1->2->NULL
Rotate right 4 steps: 2->0->1->NULL
Ideas
First connect into a circular linked list, then find the new head node, and then output
Code
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null) return null;
if (head.next == null) return head;
// 循环链表实现
ListNode old_tail = head;
int n;
for(n = 1; old_tail.next != null; n++)
old_tail = old_tail.next;
old_tail.next = head;
// 新的尾 : (n - k % n - 1)
// 新的头 : (n - k % n)
ListNode new_tail = head;
for (int i = 0; i < n - k % n - 1; i++)
new_tail = new_tail.next;
ListNode new_head = new_tail.next;
new_tail.next = null;
return new_head;
}
}