diff --git a/README.md b/README.md index 952c3851..a90ef380 100644 --- a/README.md +++ b/README.md @@ -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| \ No newline at end of file +|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| \ No newline at end of file diff --git a/src/1424-Diagonal-Traverse-II/1424.cpp b/src/1424-Diagonal-Traverse-II/1424.cpp new file mode 100644 index 00000000..d2ac2396 --- /dev/null +++ b/src/1424-Diagonal-Traverse-II/1424.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector findDiagonalOrder(vector>& nums) { + vector> mat; + int n = 0, k = 0; + for (int i = 0; i < nums.size(); i++) { + vector 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 res(n); + for (auto& diag : mat) { + for (int i = diag.size() - 1; i >= 0; i--) { + res[k++] = diag[i]; + } + } + return res; + } +}; \ No newline at end of file diff --git a/src/1424-Diagonal-Traverse-II/1424.go b/src/1424-Diagonal-Traverse-II/1424.go new file mode 100644 index 00000000..388f15f3 --- /dev/null +++ b/src/1424-Diagonal-Traverse-II/1424.go @@ -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 +} \ No newline at end of file diff --git a/src/1424-Diagonal-Traverse-II/1424.java b/src/1424-Diagonal-Traverse-II/1424.java new file mode 100644 index 00000000..19cccb35 --- /dev/null +++ b/src/1424-Diagonal-Traverse-II/1424.java @@ -0,0 +1,21 @@ +class Solution { + public int[] findDiagonalOrder(List> nums) { + List> mat = new ArrayList<>(); + int n = 0, k = 0; + for (int i = 0; i < nums.size(); i++) { + List 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 diag : mat) { + for (int num : diag) { + res[k++] = num; + } + } + return res; + } +} \ No newline at end of file diff --git a/src/1424-Diagonal-Traverse-II/1424.js b/src/1424-Diagonal-Traverse-II/1424.js new file mode 100644 index 00000000..848910a7 --- /dev/null +++ b/src/1424-Diagonal-Traverse-II/1424.js @@ -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; +}; \ No newline at end of file diff --git a/src/1424-Diagonal-Traverse-II/1424.py b/src/1424-Diagonal-Traverse-II/1424.py new file mode 100644 index 00000000..3cf0fc1f --- /dev/null +++ b/src/1424-Diagonal-Traverse-II/1424.py @@ -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]] \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index c758320c..fc469a0d 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -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']