Merge pull request '2508/FindDiagonalOrder' (#2) from 2508/FindDiagonalOrder into main
Reviewed-on: #2
This commit is contained in:
24
202508/498 FindDiagonalOrder/C#/C#.sln
Normal file
24
202508/498 FindDiagonalOrder/C#/C#.sln
Normal file
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FindDiagonalOrder", "FindDiagonalOrder.csproj", "{B3151728-C057-4305-DD4C-9A2AFBFC189A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B3151728-C057-4305-DD4C-9A2AFBFC189A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B3151728-C057-4305-DD4C-9A2AFBFC189A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B3151728-C057-4305-DD4C-9A2AFBFC189A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B3151728-C057-4305-DD4C-9A2AFBFC189A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {DB56539A-6F32-4D28-B91F-BB1DCF46F4D0}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
10
202508/498 FindDiagonalOrder/C#/FindDiagonalOrder.csproj
Normal file
10
202508/498 FindDiagonalOrder/C#/FindDiagonalOrder.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
61
202508/498 FindDiagonalOrder/C#/Program.cs
Normal file
61
202508/498 FindDiagonalOrder/C#/Program.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
public class Solution {
|
||||
public int[] FindDiagonalOrder(int[][] mat)
|
||||
{
|
||||
if (mat == null || mat.Length == 0 || mat[0].Length == 0)
|
||||
{
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
var m = mat.Length;
|
||||
var n = mat[0].Length;
|
||||
|
||||
var diagonals = new Dictionary<int, List<int>>();
|
||||
|
||||
for (var i = 0; i < m; i++)
|
||||
{
|
||||
for (var j = 0; j < n; j++)
|
||||
{
|
||||
int diagonalIndex = i + j;
|
||||
if (!diagonals.ContainsKey(diagonalIndex))
|
||||
{
|
||||
diagonals[diagonalIndex] = new List<int>();
|
||||
}
|
||||
diagonals[diagonalIndex].Add(mat[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
var result = new List<int>();
|
||||
foreach (var key in diagonals.Keys)
|
||||
{
|
||||
var diagonal = diagonals[key];
|
||||
if (key % 2 == 0)
|
||||
{
|
||||
result.AddRange(diagonal);
|
||||
}
|
||||
else
|
||||
{
|
||||
diagonal.Reverse();
|
||||
result.AddRange(diagonal);
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
var Solution = new Solution();
|
||||
|
||||
int[][] mat1 = new int[][]{
|
||||
new int[]{1,2,3},
|
||||
new int[]{4,5,6},
|
||||
new int[]{7,8,9}
|
||||
};
|
||||
|
||||
Console.WriteLine(string.Join(", ", Solution.FindDiagonalOrder(mat1)));
|
||||
|
||||
}
|
||||
}
|
45
202508/498 FindDiagonalOrder/Go/main.go
Normal file
45
202508/498 FindDiagonalOrder/Go/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func findDiagonalOrder(mat [][]int) []int {
|
||||
if len(mat) == 0 || len(mat[0]) == 0{
|
||||
return []int{}
|
||||
}
|
||||
|
||||
m := len(mat)
|
||||
n := len(mat[0])
|
||||
|
||||
// maps
|
||||
diagonals := make(map[int][]int)
|
||||
|
||||
for i:=0; i< m;i++{
|
||||
for j:=0; j < n; j++{
|
||||
index := i+j
|
||||
diagonals[index] = append(diagonals[index], mat[i][j])
|
||||
}
|
||||
}
|
||||
|
||||
result := []int{}
|
||||
|
||||
for i:= 0; i< m+n-1; i++{
|
||||
if i% 2 == 0{
|
||||
reverse(diagonals[i])
|
||||
result = append(result, diagonals[i]...)
|
||||
}else{
|
||||
result = append(result, diagonals[i]...)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func reverse(slice []int){
|
||||
left, right :=0, len(slice)-1
|
||||
|
||||
for left < right{
|
||||
slice[left], slice[right] = slice[right], slice[left]
|
||||
left++
|
||||
right--
|
||||
}
|
||||
}
|
7
202508/498 FindDiagonalOrder/solution.md
Normal file
7
202508/498 FindDiagonalOrder/solution.md
Normal file
@@ -0,0 +1,7 @@
|
||||
1. 觀察矩陣對角線模式
|
||||
1. (0,0) -> 1 -> i+j = 0
|
||||
2. (1, 0), (0, 1) -> 2 -> i+j = 1
|
||||
3. (2, 0), (1, 1), (0, 2) -> 3 -> i+j = 2
|
||||
2. how to deal with diagonal sorting?
|
||||
1. while (i+j) % 2 == 1
|
||||
2. while (i+j) % 2 == 0 reversed
|
Reference in New Issue
Block a user