forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a99ec8
commit eac3cf0
Showing
7 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
public: | ||
vector<int> findDiagonalOrder(vector<vector<int>>& nums) { | ||
vector<vector<int>> mat; | ||
int n = 0, k = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
vector<int> row = nums[i]; | ||
for (int j = 0; j < row.size(); j++, n++) { | ||
if (i + j >= mat.size()) mat.push_back({}); | ||
mat[i + j].emplace_back(row[j]); | ||
} | ||
} | ||
|
||
vector<int> res(n); | ||
for (auto& diag : mat) { | ||
for (int i = diag.size() - 1; i >= 0; i--) { | ||
res[k++] = diag[i]; | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
func findDiagonalOrder(nums [][]int) []int { | ||
mat := [][]int{} | ||
for i := 0; i < len(nums); i++ { | ||
for j := 0; j < len(nums[i]); j++ { | ||
if i + j >= len(mat) { | ||
mat = append(mat, []int{}) | ||
} | ||
mat[i + j] = append(mat[i + j], nums[i][j]) | ||
} | ||
} | ||
|
||
res := []int{} | ||
for _, diag := range mat { | ||
for i := len(diag) - 1; i >= 0; i-- { | ||
res = append(res, diag[i]) | ||
} | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public int[] findDiagonalOrder(List<List<Integer>> nums) { | ||
List<Deque<Integer>> mat = new ArrayList<>(); | ||
int n = 0, k = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
List<Integer> row = nums.get(i); | ||
for (int j = 0; j < row.size(); j++, n++) { | ||
if (i + j >= mat.size()) mat.add(new ArrayDeque()); | ||
mat.get(i + j).push(row.get(j)); | ||
} | ||
} | ||
|
||
int[] res = new int[n]; | ||
for (Deque<Integer> diag : mat) { | ||
for (int num : diag) { | ||
res[k++] = num; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var findDiagonalOrder = function(nums) { | ||
let mat = []; | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = 0; j < nums[i].length; j++) { | ||
if (i + j >= mat.length) mat.push([]); | ||
mat[i + j].unshift(nums[i][j]); | ||
} | ||
} | ||
|
||
let res = []; | ||
for (let diag of mat) { | ||
for (let num of diag) { | ||
res.push(num); | ||
} | ||
} | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Solution: | ||
def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: | ||
res = [] | ||
for i, r in enumerate(nums): | ||
for j, a in enumerate(r): | ||
if len(res) <= i + j: | ||
res.append([]) | ||
res[i + j].append(a) | ||
return [a for r in res for a in r[::-1]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters