Skip to content

Commit 23a403b

Browse files
committed
new soln
1 parent efdaa12 commit 23a403b

5 files changed

+218
-5
lines changed

36.IsValidSudoku.cs

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1-
// Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1+
// 36. Valid Sudoku
22

3+
// Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
34
// Each row must contain the digits 1-9 without repetition.
45
// Each column must contain the digits 1-9 without repetition.
56
// Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
67
// Note:
7-
88
// A Sudoku board (partially filled) could be valid but is not necessarily solvable.
99
// Only the filled cells need to be validated according to the mentioned rules.
10+
11+
12+
// Example 1:
13+
// Input: board =
14+
// [["5","3",".",".","7",".",".",".","."]
15+
// ,["6",".",".","1","9","5",".",".","."]
16+
// ,[".","9","8",".",".",".",".","6","."]
17+
// ,["8",".",".",".","6",".",".",".","3"]
18+
// ,["4",".",".","8",".","3",".",".","1"]
19+
// ,["7",".",".",".","2",".",".",".","6"]
20+
// ,[".","6",".",".",".",".","2","8","."]
21+
// ,[".",".",".","4","1","9",".",".","5"]
22+
// ,[".",".",".",".","8",".",".","7","9"]]
23+
// Output: true
24+
// Example 2:
25+
// Input: board =
26+
// [["8","3",".",".","7",".",".",".","."]
27+
// ,["6",".",".","1","9","5",".",".","."]
28+
// ,[".","9","8",".",".",".",".","6","."]
29+
// ,["8",".",".",".","6",".",".",".","3"]
30+
// ,["4",".",".","8",".","3",".",".","1"]
31+
// ,["7",".",".",".","2",".",".",".","6"]
32+
// ,[".","6",".",".",".",".","2","8","."]
33+
// ,[".",".",".","4","1","9",".",".","5"]
34+
// ,[".",".",".",".","8",".",".","7","9"]]
35+
// Output: false
36+
// Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8.
37+
// Since there are two 8's in the top left 3x3 sub-box, it is invalid.
38+
39+
// Constraints:
40+
// board.length == 9
41+
// board[i].length == 9
42+
// board[i][j] is a digit 1-9 or '.'.
1043

1144
public class Solution {
1245
public bool IsValidSudoku(char[][] board)

37.SolveSudoku.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// 37. Sudoku Solver
2+
// Write a program to solve a Sudoku puzzle by filling the empty cells.
3+
4+
// A sudoku solution must satisfy all of the following rules:
5+
6+
// Each of the digits 1-9 must occur exactly once in each row.
7+
// Each of the digits 1-9 must occur exactly once in each column.
8+
// Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
9+
// The '.' character indicates empty cells.
10+
11+
// Example 1:
12+
13+
// Input: board =
14+
// [["5","3",".",".","7",".",".",".","."],
15+
// ["6",".",".","1","9","5",".",".","."],
16+
// [".","9","8",".",".",".",".","6","."],
17+
// ["8",".",".",".","6",".",".",".","3"],
18+
// ["4",".",".","8",".","3",".",".","1"],
19+
// ["7",".",".",".","2",".",".",".","6"],
20+
// [".","6",".",".",".",".","2","8","."],
21+
// [".",".",".","4","1","9",".",".","5"],
22+
// [".",".",".",".","8",".",".","7","9"]]
23+
// Output:
24+
// [["5","3","4","6","7","8","9","1","2"],
25+
// ["6","7","2","1","9","5","3","4","8"],
26+
// ["1","9","8","3","4","2","5","6","7"],
27+
// ["8","5","9","7","6","1","4","2","3"],
28+
// ["4","2","6","8","5","3","7","9","1"],
29+
// ["7","1","3","9","2","4","8","5","6"],
30+
// ["9","6","1","5","3","7","2","8","4"],
31+
// ["2","8","7","4","1","9","6","3","5"],
32+
// ["3","4","5","2","8","6","1","7","9"]]
33+
// Explanation: The input board is shown above and the only valid solution is shown below:
34+
35+
// Constraints:
36+
// board.length == 9
37+
// board[i].length == 9
38+
// board[i][j] is a digit or '.'.
39+
// It is guaranteed that the input board has only one solution.
40+
41+
//Time: O(9^m); m is number of empty cells
42+
//Space: O(m)
43+
public class Solution {
44+
public void SolveSudoku(char[][] board) {
45+
SudokuPossible(board);
46+
}
47+
48+
private bool SudokuPossible(char[][] board) {
49+
for (int i = 0; i < 9; i++) {
50+
for (int j = 0; j < 9; j++) {
51+
if (board[i][j] == '.') {
52+
for (char n = '1'; n <= '9'; n++) {
53+
if (IsValid(board, i, j, n)) {
54+
board[i][j] = n;
55+
if (SudokuPossible(board)) {
56+
return true;
57+
}
58+
board[i][j] = '.';
59+
}
60+
}
61+
return false; // Backtrack if no valid number is found
62+
}
63+
}
64+
}
65+
return true; // Return true if the entire board is filled correctly
66+
}
67+
68+
private bool IsValid(char[][] board, int row, int col, char num) {
69+
// Check row
70+
for (int i = 0; i < 9; i++) {
71+
if (board[row][i] == num) {
72+
return false;
73+
}
74+
}
75+
// Check column
76+
for (int i = 0; i < 9; i++) {
77+
if (board[i][col] == num) {
78+
return false;
79+
}
80+
}
81+
// Check 3x3 sub-box
82+
int boxRowStart = (row / 3) * 3;
83+
int boxColStart = (col / 3) * 3;
84+
for (int i = 0; i < 3; i++) {
85+
for (int j = 0; j < 3; j++) {
86+
if (board[boxRowStart + i][boxColStart + j] == num) {
87+
return false;
88+
}
89+
}
90+
}
91+
return true;
92+
}
93+
}

46.Permute.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
// Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
44

55
// Example 1:
6-
76
// Input: nums = [1,2,3]
87
// Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
98
// Example 2:
10-
119
// Input: nums = [0,1]
1210
// Output: [[0,1],[1,0]]
1311
// Example 3:
14-
1512
// Input: nums = [1]
1613
// Output: [[1]]
1714

@@ -34,6 +31,7 @@ public void FindPermutaion
3431
for(int i = 0; i < nums.Count; i++){
3532
IList<int> temp = new List<int>(nums);
3633
permut.Add(nums[i]);
34+
//remove used number
3735
temp.RemoveAt(i);
3836
FindPermutaion(temp,permut,result);
3937
permut.Remove(nums[i]);

47.PermuteUnique.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 47. Permutations II
2+
3+
// Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
4+
// Example 1:
5+
// Input: nums = [1,1,2]
6+
// Output:
7+
// [[1,1,2],
8+
// [1,2,1],
9+
// [2,1,1]]
10+
// Example 2:
11+
// Input: nums = [1,2,3]
12+
// Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
13+
14+
// Constraints:
15+
// 1 <= nums.length <= 8
16+
// -10 <= nums[i] <= 10
17+
18+
public class Solution {
19+
public IList<IList<int>> PermuteUnique(int[] nums) {
20+
// Sort the numbers to handle duplicates easily
21+
Array.Sort(nums);
22+
List<IList<int>> result = new List<IList<int>>();
23+
bool[] used = new bool[nums.Length];
24+
List<int> permute = new List<int>();
25+
FindPermute(nums, permute, used, result);
26+
return result;
27+
}
28+
29+
private void FindPermute(int[] nums, List<int> permute, bool[] used, List<IList<int>> result) {
30+
if (permute.Count == nums.Length) {
31+
result.Add(new List<int>(permute));
32+
return;
33+
}
34+
for (int i = 0; i < nums.Length; i++) {
35+
// Skip used numbers or duplicates
36+
if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) {
37+
continue;
38+
}
39+
used[i] = true;
40+
permute.Add(nums[i]);
41+
FindPermute(nums, permute, used, result);
42+
used[i] = false;
43+
permute.RemoveAt(permute.Count - 1);
44+
}
45+
}
46+
}

503.NextGreaterElements.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 503. Next Greater Element II
2+
// Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.
3+
// The next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return -1 for this number.
4+
5+
// Example 1:
6+
// Input: nums = [1,2,1]
7+
// Output: [2,-1,2]
8+
// Explanation: The first 1's next greater number is 2;
9+
// The number 2 can't find next greater number.
10+
// The second 1's next greater number needs to search circularly, which is also 2.
11+
// Example 2:
12+
// Input: nums = [1,2,3,4,3]
13+
// Output: [2,3,4,-1,4]
14+
15+
// Constraints:
16+
// 1 <= nums.length <= 104
17+
// -109 <= nums[i] <= 109
18+
19+
public class Solution {
20+
public int[] NextGreaterElements(int[] nums) {
21+
int n = nums.Length;
22+
int[] result = new int[n];
23+
Stack<int> st = new();
24+
25+
// Initialize the result array with -1
26+
for (int i = 0; i < n; i++) {
27+
result[i] = -1;
28+
}
29+
30+
// Process each element twice to handle the circular aspect
31+
for (int i = 2 * n - 1; i >= 0; i--) {
32+
while (st.Any() && nums[st.Peek()] <= nums[i % n]) {
33+
st.Pop();
34+
}
35+
if (st.Any()) {
36+
result[i % n] = nums[st.Peek()];
37+
}
38+
st.Push(i % n);
39+
}
40+
41+
return result;
42+
}
43+
}

0 commit comments

Comments
 (0)