C++
LeetCode
Medium
= exmaple 1 =
matrix =
{
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
}
每一次 loop 就是走一圈
也就是第一圈是
1 2 3 4 5 12 11 10 9 5
第二圈是
6 7
每一圈又分為四個步驟
上 -> 右 -> 下 -> 左
第一圈 :
1 2 3 4 -> 8 12 -> 11 10 9 -> 5
第二圈 :
6 7 -> x -> x -> x
可以注意到在後兩個 for 迴圈有一個 break 的條件
如果 n == 0 或 m == 0 的話
代表這一輪的一圈並不是一圈
而只是一個直排
或一個橫排
或一個點
因此只需要一個 for 迴圈有一個
如果兩個 for 迴圈都用上
假設這剩下的一行是 1 2 3 4
那走兩次會是 1 2 3 4 3 2 1
這並不正確 所以要 break
#include <iostream>
#include "cout.h"
vector<int> spiralOrder(vector<vector<int>>& matrix);
int main()
{
vector<vector<int>> matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
coutVector(spiralOrder(matrix));
return 0;
}
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector<int> result;
int m = matrix.size();
int n = matrix[0].size();
int loop = 0;
int left = 0;
int right = 0;
int top = 0;
int bottom = 0;
while(m > 0 && n > 0)
{
left = loop;
top = loop;
right = loop + n - 1;
bottom = loop + m -1;
for(int j = left; j <= right; j++)
{
result.push_back(matrix[top][j]);
}
for(int i = top + 1; i <= bottom; i++)
{
result.push_back(matrix[i][right]);
}
for(int j = right - 1; j >= left; j--)
{
if(m - 1 <= 0) break; /* no lines horizontal left to be processed */
result.push_back(matrix[bottom][j]);
}
for(int i = bottom - 1; i > top; i--)
{
if(n - 1 <= 0) break; /* no vertical lines left to be processed */
result.push_back(matrix[i][left]);
}
m = m - 2;
n = n - 2;
loop++;
}
return result;
}
1 - 100 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 5. Longest Palindromic Substring 6. Zigzag Conversion 7. Reverse Integer 8. String to Integer (atoi) 9. Palindrome Number
Dec 1, 2022Notes vector 裡存的是路徑行經的檔案夾名稱 往下一層資料夾往下走就 push 進 vector 如果遇到 .. 就把尾端的檔案夾名稱 pop 掉 Code #include <vector> #include <string> #include "cout.h"
Nov 30, 2022Notes 向右轉 k 個代表倒數第 k 個 node 要做新的 head 第 k - 1 個 node 要成為新的尾端, next 指向 null 原本的尾端變成一個普通的 node, next 指向原本的 head Code #include "cout.h" ListNode* rotateRight(ListNode* head, int k);
Nov 29, 2022Notes 使用 DP + memo 可以避免重複計算 memo[x][y] 指的是從位置 (0, 0) 到位置 (x, y) 所需要花費的最少的錢 Code #include <vector> #include <algorithm> #include <climits> #include "cout.h"
Nov 29, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up