refactor: refactor project structure

This commit is contained in:
2025-09-01 09:13:32 +08:00
parent 524784a3c5
commit 9fec047d3f
42 changed files with 0 additions and 4 deletions

View 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

View 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>

View 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)));
}
}

View 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--
}
}

View 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