LeetCode50题(17天)-Day7
54 螺旋矩阵
- 题号:54
- 难度:中等
- https://leetcode-cn.com/problems/spiral-matrix/
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
示例 3:
输入:
[
[1]
]
输出: [1]
示例 4:
输入:
[
[2, 3, 4],
[5, 6, 7],
[8, 9, 10],
[11, 12, 13]
]
输出: [2,3,4,7,10,13,12,11,8,5,6,9]
实现
- 状态:通过
- 22 / 22 个通过测试用例
- 执行用时: 348 ms, 在所有 C# 提交中击败了 85.53% 的用户
- 内存消耗: 28.9 MB, 在所有 C# 提交中击败了 5.26% 的用户
public class Solution
{
public IList<int> SpiralOrder(int[][] matrix)
{
IList<int> result = new List<int>();
if (matrix == null || matrix.Length == 0)
return result;
int start = 0;
int end1 = matrix[start].Length - 1 - start;
int end2 = matrix.Length - 1 - start;
// 只有横着的情况
if (start == end2)
{
LeftToRight(start, end1, start, matrix, result);
return result;
}
//只有竖着的情况
if (start == end1)
{
TopToBottom(start, end2, start, matrix, result);
return result;
}
while (start < end1 && start < end2)
{
LeftToRight(start, end1, start, matrix, result);
TopToBottom(start + 1, end2, end1, matrix, result);
RightToLeft(end1 - 1, start, end2, matrix, result);
BottomToTop(end2 - 1, start + 1, start, matrix, result);
start++;
end1 = matrix[start].Length - 1 - start;
end2 = matrix.Length - 1 - start;
}
// 只剩下横着的情况
if (start == end2)
{
LeftToRight(start, end1, start, matrix, result);
}
else if (start == end1)
{
//只剩下竖着的情况
TopToBottom(start, end2, start, matrix, result);
}
return result;
}
private void LeftToRight(int start, int end, int rowIndex, int[][] matrix, IList<int> lst)
{
for (int i = start; i <= end; i++)
{
lst.Add(matrix[rowIndex][i]);
}
}
private void TopToBottom(int start, int end, int colIndex, int[][] matrix, IList<int> lst)
{
for (int i = start; i <= end; i++)
{
lst.Add(matrix[i][colIndex]);
}
}
private void RightToLeft(int start, int end, int rowIndex, int[][] matrix, IList<int> lst)
{
for (int i = start; i >= end; i--)
{
lst.Add(matrix[rowIndex][i]);
}
}
private void BottomToTop(int start, int end, int colIndex, int[][] matrix, IList<int> lst)
{
for (int i = start; i >= end; i--)
{
lst.Add(matrix[i][colIndex]);
}
}
}
59 螺旋矩阵2
- 题号:59
- 难度:中等
- https://leetcode-cn.com/problems/spiral-matrix-ii/
给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
示例:
输入: 3
输出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
实现
- 状态:通过
- 20 / 20 个通过测试用例
- 执行用时: 296 ms, 在所有 C# 提交中击败了 97.67% 的用户
- 内存消耗: 25 MB, 在所有 C# 提交中击败了 11.11% 的用户
public class Solution
{
public int[][] GenerateMatrix(int n)
{
int[][] matrix = new int[n][];
for (int i = 0; i < n; i++)
{
matrix[i] = new int[n];
}
int start = 0;//起始位置
int end1 = n - 1;//最左边位置
int end2 = n - 1;//最下边位置
int count = 1;
while (start < end1 && start < end2)
{
LeftToRight(start, end1, start, matrix, ref count);
TopToBottom(start + 1, end2, end1, matrix, ref count);
RightToLeft(end1 - 1, start, end2, matrix, ref count);
BottomToTop(end2 - 1, start + 1, start, matrix, ref count);
start++;
end1 = n - 1 - start;
end2 = n - 1 - start;
}
if (n%2 == 1)
{
matrix[start][start] = count;
}
return matrix;
}
private void LeftToRight(int start, int end, int rowIndex, int[][] matrix, ref int from)
{
for (int i = start; i <= end; i++)
{
matrix[rowIndex][i] = from;
from++;
}
}
private void TopToBottom(int start, int end, int colIndex, int[][] matrix, ref int from)
{
for (int i = start; i <= end; i++)
{
matrix[i][colIndex] = from;
from++;
}
}
private void RightToLeft(int start, int end, int rowIndex, int[][] matrix, ref int from)
{
for (int i = start; i >= end; i--)
{
matrix[rowIndex][i] = from;
from++;
}
}
private void BottomToTop(int start, int end, int colIndex, int[][] matrix, ref int from)
{
for (int i = start; i >= end; i--)
{
matrix[i][colIndex] = from;
from++;
}
}
}
61 旋转链表
- 题号:61
- 难度:中等
- https://leetcode-cn.com/problems/rotate-list/
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:
输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL
实现
- 执行结果:通过
- 执行用时:100 ms, 在所有 C# 提交中击败了 98.13% 的用户
- 内存消耗:25.1 MB, 在所有 C# 提交中击败了 100.00% 的用户
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode RotateRight(ListNode head, int k)
{
if (head == null || k == 0)
return head;
int len = GetLength(head);
int index = len - k%len;
if (index == len)
return head;
ListNode temp1 = head;
ListNode temp2 = head;
for (int i = 0; i < index - 1; i++)
{
temp1 = temp1.next;
}
head = temp1.next;
temp1.next = null;
temp1 = head;
while (temp1.next != null)
{
temp1 = temp1.next;
}
temp1.next = temp2;
return head;
}
public int GetLength(ListNode head)
{
ListNode temp = head;
int i = 0;
while (temp != null)
{
i++;
temp = temp.next;
}
return i;
}
}