Skip to content

Commit 433cbc8

Browse files
committed
new soln
1 parent 036234d commit 433cbc8

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

153.FindMin.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 153. Find Minimum in Rotated Sorted Array
2+
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
3+
// [4,5,6,7,0,1,2] if it was rotated 4 times.
4+
// [0,1,2,4,5,6,7] if it was rotated 7 times.
5+
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
6+
// Given the sorted rotated array nums of unique elements, return the minimum element of this array.
7+
// You must write an algorithm that runs in O(log n) time.
8+
// Example 1:
9+
// Input: nums = [3,4,5,1,2]
10+
// Output: 1
11+
// Explanation: The original array was [1,2,3,4,5] rotated 3 times.
12+
// Example 2:
13+
// Input: nums = [4,5,6,7,0,1,2]
14+
// Output: 0
15+
// Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
16+
// Example 3:
17+
// Input: nums = [11,13,15,17]
18+
// Output: 11
19+
// Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
20+
21+
public class Solution {
22+
public int FindMin(int[] nums) {
23+
int left = 0,
24+
right = nums.Length-1;
25+
while(left < right){
26+
if(nums[left] < nums[right])
27+
return nums[left];
28+
int mid = (right - left)/2 + left;
29+
if(nums[mid] < nums[left])
30+
right = mid;
31+
else
32+
left = mid+1;
33+
}
34+
return nums[left];
35+
}
36+
}

154.FindMin.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 154. Find Minimum in Rotated Sorted Array II
2+
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:
3+
// [4,5,6,7,0,1,4] if it was rotated 4 times.
4+
// [0,1,4,4,5,6,7] if it was rotated 7 times.
5+
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
6+
// Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.
7+
// You must decrease the overall operation steps as much as possible.
8+
// Example 1:
9+
// Input: nums = [1,3,5]
10+
// Output: 1
11+
// Example 2:
12+
// Input: nums = [2,2,2,0,1]
13+
// Output: 0
14+
15+
public class Solution {
16+
public int FindMin(int[] nums) {
17+
Array.Sort(nums);
18+
return nums[0];
19+
}
20+
}
21+
22+
// nums[lo] <= nums[mi] <= nums[hi], min is nums[lo]
23+
// nums[lo] > nums[mi] <= nums[hi], (lo, mi] is not sorted, min is inside
24+
// nums[lo] <= nums[mi] > nums[hi], (mi, hi] is not sorted, min is inside
25+
// nums[lo] > nums[mi] > nums[hi], impossible
26+
27+
public int FindMin(int[] nums) {
28+
int left = 0,
29+
right = nums.Length-1;
30+
while(left < right){
31+
int mid = (right - left)/2 + left;
32+
if(nums[mid] > nums[right])
33+
left = mid + 1;
34+
else if (nums[mid] < nums[left])
35+
right = mid;
36+
else{
37+
right--;
38+
}
39+
}
40+
return nums[left];
41+
}

17.LetterCombinations.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 17. Letter Combinations of a Phone Number
2+
// Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
3+
// A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
4+
// Example 1:
5+
// Input: digits = "23"
6+
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
7+
// Example 2:
8+
// Input: digits = ""
9+
// Output: []
10+
// Example 3:
11+
// Input: digits = "2"
12+
// Output: ["a","b","c"]
13+
14+
public class Solution {
15+
string[] dialPad = new string[]
16+
{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
17+
public IList<string> LetterCombinations(string digits) {
18+
IList<string> result = new List<string>();
19+
if(digits != ""){
20+
AllCombination(result,digits,"", 0);
21+
}
22+
return result;
23+
}
24+
public void AllCombination(IList<string> result,string digits,string s, int idx) {
25+
if(idx == digits.Length){
26+
result.Add(new string(s));
27+
return;
28+
}
29+
int num = digits[idx] - '0';
30+
foreach(char c in dialPad[num]){
31+
AllCombination(result,digits,s + c, idx+1);
32+
}
33+
}
34+
}

36.IsValidSudoku.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,35 @@ public bool CheckBox(char[][] board, int row, int col)
6464
}
6565
return true;
6666
}
67+
}
68+
69+
//HasSet
70+
public class Solution {
71+
public bool IsValidSudoku(char[][] board) {
72+
HashSet<int>[] row = new HashSet<int>[9];
73+
HashSet<int>[] col = new HashSet<int>[9];
74+
HashSet<int>[] box = new HashSet<int>[9];
75+
for(int i = 0; i < 9; i++){
76+
row[i] = new HashSet<int>();
77+
col[i] = new HashSet<int>();
78+
box[i] = new HashSet<int>();
79+
}
80+
for(int i = 0; i < 9; i++){
81+
for(int j = 0; j < 9; j++){
82+
if(board[i][j] != '.'){
83+
int val = board[i][j] - '0';
84+
if(!row[i].Contains(val) && !col[j].Contains(val)
85+
&& !box[(i/3)*3 + (j/3)].Contains(val)) {
86+
row[i].Add(val);
87+
col[j].Add(val);
88+
box[(i/3)*3 + (j/3)].Add(val);
89+
}
90+
else{
91+
return false;
92+
}
93+
}
94+
}
95+
}
96+
return true;
97+
}
6798
}

0 commit comments

Comments
 (0)