Merge pull request #46 from JZFamily/master

push to kiritow
This commit is contained in:
Kirigaya Kazuto 2018-07-07 08:03:14 +08:00 committed by GitHub
commit 69f74c257b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2634 additions and 0 deletions

View File

@ -0,0 +1,159 @@
## 48. 旋转图像@2018/06/24
### jzf
* 4ms
```c++
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int tmp;
for(size_t i=0; i<matrix.size();i++)
{
for(size_t j=i+1;j<matrix.size();j++)
{
tmp =matrix[i][j];
matrix[i][j] =matrix[j][i];
matrix[j][i] =tmp;
}
}
for(size_t i =0;i<matrix.size();i++)
{
reverse(matrix[i].begin(),matrix[i].end());
}
}
};
```
### kiritow
* 4ms
```c++
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
if (n % 2) // odd number, easy
{
int mid = n / 2;
for (int dis = 1; dis <= mid; dis++)
{
// do chain swap at distance:dis
int swap_n = 2 * dis + 1;
// save right col
vector<int> tvec;
int begin_pos = mid - dis;
for (int i = 0; i < swap_n; i++)
{
tvec.push_back(matrix[begin_pos + i][mid + dis]);
}
// start rotate
// RIGHT <- UPPER
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid + dis, mid - dis, begin_pos + i);
matrix[begin_pos + i][mid + dis] = matrix[mid - dis][begin_pos + i];
}
// UPPER <- LEFT
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", mid - dis, begin_pos + i, begin_pos + i, mid - dis);
matrix[mid - dis][begin_pos + (swap_n - i - 1)] = matrix[begin_pos + i][mid - dis];
}
// LEFT <- DOWNER
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid - dis, mid + dis, begin_pos + i);
matrix[begin_pos + i][mid - dis] = matrix[mid + dis][begin_pos + i];
}
// DOWNER <- TEMP
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-tvec[%d]\n", mid + dis, begin_pos + i, i);
matrix[mid + dis][begin_pos + (swap_n - i - 1)] = tvec[i];
}
// debug
//print(matrix);
}
}
else // oh, this stuff sucks.
{
int mid = n / 2 ;
for (int dis = 1; dis <= mid ; dis++)
{
// do chain swap at distance:dis
int swap_n = 2 * dis;
// save right col
vector<int> tvec;
int begin_pos = mid - dis;
for (int i = 0; i < swap_n; i++)
{
tvec.push_back(matrix[begin_pos + i][mid + dis - 1]);
}
// start rotate
// RIGHT <- UPPER
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid + dis, mid - dis, begin_pos + i);
matrix[begin_pos + i][mid + dis - 1] = matrix[mid - dis][begin_pos + i];
}
// UPPER <- LEFT
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", mid - dis, begin_pos + i, begin_pos + i, mid - dis);
matrix[mid - dis][begin_pos + (swap_n - i - 1)] = matrix[begin_pos + i][mid - dis];
}
// LEFT <- DOWNER
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid - dis, mid + dis, begin_pos + i);
matrix[begin_pos + i][mid - dis] = matrix[mid + dis -1][begin_pos + i];
}
// DOWNER <- TEMP
for (int i = 0; i < swap_n; i++)
{
//printf("matrix[%d][%d]<-tvec[%d]\n", mid + dis, begin_pos + i, i);
matrix[mid + dis -1][begin_pos + (swap_n - i - 1)] = tvec[i];
}
// debug
//print(matrix);
}
}
}
};
```
### puck
class Solution
{
public:
void rotate(vector<vector<int>> &matrix)
{
auto n = matrix.size();
auto max = n - 1;
auto half = n / 2;
for (int i{}; i < half; ++i)
{
for (int j{}; j < half; ++j)
{
std::swap(matrix[i][j], matrix[j][max - i]);
std::swap(matrix[i][j], matrix[max - i][max - j]);
std::swap(matrix[i][j], matrix[max - j][i]);
}
}
if (n == 2 * half + 1)
{
for (int i{}; i < half; ++i)
{
std::swap(matrix[i][half], matrix[half][max - i]);
std::swap(matrix[i][half], matrix[max - i][half]);
std::swap(matrix[i][half], matrix[half][i]);
}
}
}
};
```

2475
LeetCode-CN/刷题.md Normal file

File diff suppressed because it is too large Load Diff