Skip to content

Commit

Permalink
add 1424
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed May 4, 2020
1 parent 6a99ec8 commit eac3cf0
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,5 @@ LeetCode
|1419|[Minimum Number of Frogs Croaking](https://leetcode.com/problems/minimum-number-of-frogs-croaking/)|c|[c++](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.cpp)|[python](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.py)|[go](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.go)|[js](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.js)|[java](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.java)|Medium|
|1420|[Build Array Where You Can Find The Maximum Exactly K Comparisons](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/)|c|[c++](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp)|[python](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py)|[go](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go)|[js](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js)|[java](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java)|Hard|
|1422|[Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string/)|c|[c++](./src/1422-Maximum-Score-After-Splitting-a-String/1422.cpp)|[python](./src/1422-Maximum-Score-After-Splitting-a-String/1422.py)|[go](./src/1422-Maximum-Score-After-Splitting-a-String/1422.go)|[js](./src/1422-Maximum-Score-After-Splitting-a-String/1422.js)|[java](./src/1422-Maximum-Score-After-Splitting-a-String/1422.java)|Easy|
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|c|[c++](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.cpp)|[python](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.py)|[go](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.go)|[js](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.js)|[java](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.java)|Medium|
|1423|[Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/)|c|[c++](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.cpp)|[python](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.py)|[go](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.go)|[js](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.js)|[java](./src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.java)|Medium|
|1424|[Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii/)|c|[c++](./src/1424-Diagonal-Traverse-II/1424.cpp)|[python](./src/1424-Diagonal-Traverse-II/1424.py)|[go](./src/1424-Diagonal-Traverse-II/1424.go)|[js](./src/1424-Diagonal-Traverse-II/1424.js)|[java](./src/1424-Diagonal-Traverse-II/1424.java)|Medium|
22 changes: 22 additions & 0 deletions src/1424-Diagonal-Traverse-II/1424.cpp
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;
}
};
19 changes: 19 additions & 0 deletions src/1424-Diagonal-Traverse-II/1424.go
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
}
21 changes: 21 additions & 0 deletions src/1424-Diagonal-Traverse-II/1424.java
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;
}
}
17 changes: 17 additions & 0 deletions src/1424-Diagonal-Traverse-II/1424.js
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;
};
9 changes: 9 additions & 0 deletions src/1424-Diagonal-Traverse-II/1424.py
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]]
6 changes: 3 additions & 3 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os, bisect

# 题目名称
name = "Maximum Points You Can Obtain from Cards"
ID = 1423
url = "https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/"
name = "Diagonal Traverse II"
ID = 1424
url = "https://leetcode.com/problems/diagonal-traverse-ii/"
difficult = "Medium"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']

Expand Down

0 comments on commit eac3cf0

Please sign in to comment.