Skip to content

Commit 83c9061

Browse files
committed
clean up
1 parent 116c212 commit 83c9061

7 files changed

+87
-22
lines changed

66.PlusOne.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
// 66. Plus One
2-
// You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
2+
// You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer.
3+
// The digits are ordered from most significant to least significant in left-to-right order.
4+
// The large integer does not contain any leading 0's.
35
// Increment the large integer by one and return the resulting array of digits.
46

7+
// Example 1:
8+
// Input: digits = [1,2,3]
9+
// Output: [1,2,4]
10+
// Explanation: The array represents the integer 123.
11+
// Incrementing by one gives 123 + 1 = 124.
12+
// Thus, the result should be [1,2,4].
13+
// Example 2:
14+
// Input: digits = [4,3,2,1]
15+
// Output: [4,3,2,2]
16+
// Explanation: The array represents the integer 4321.
17+
// Incrementing by one gives 4321 + 1 = 4322.
18+
// Thus, the result should be [4,3,2,2].
19+
// Example 3:
20+
// Input: digits = [9]
21+
// Output: [1,0]
22+
// Explanation: The array represents the integer 9.
23+
// Incrementing by one gives 9 + 1 = 10.
24+
// Thus, the result should be [1,0].
25+
26+
public class Solution {
27+
public int[] PlusOne(int[] digits) {
28+
for (int i = digits.Length - 1; i >= 0; i--) {
29+
if (digits[i] != 9) {
30+
digits[i]++;
31+
return digits;
32+
}
33+
digits[i] = 0;
34+
}
35+
int[] result = new int[digits.Length + 1];
36+
result[0] = 1;
37+
return result;
38+
}
39+
}
40+
41+
542
public class Solution {
643
public int[] PlusOne(int[] digits) {
744
for (int i= digits.Length-1;i>=0;i--){

73.SetZeroes.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// 73. Set Matrix Zeroes
22

33
// Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
4-
54
// You must do it in place.
65

76
// Example 1:
8-
97
// Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
108
// Output: [[1,0,1],[0,0,0],[1,0,1]]
119
// Example 2:
12-
1310
// Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
1411
// Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
1512

74.SearchMatrix.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 74. Search a 2D Matrix
2-
// Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:
2+
// Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix.
3+
//This matrix has the following properties:
34

45
// Integers in each row are sorted from left to right.
56
// The first integer of each row is greater than the last integer of the previous row.

78.Subsets.cs

-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
// 78. Subsets
22

33
// Given an integer array nums of unique elements, return all possible subsets (the power set).
4-
54
// The solution set must not contain duplicate subsets. Return the solution in any order.
65

7-
8-
96
// Example 1:
10-
117
// Input: nums = [1,2,3]
128
// Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
139
// Example 2:
14-
1510
// Input: nums = [0]
1611
// Output: [[],[0]]
1712

18-
19-
2013
public class Solution {
2114
public IList<IList<int>> Subsets(int[] nums) {
2215
int pow = (int)Math.Pow(2, nums.Length);

79.Exist.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@
22

33
// Given an m x n grid of characters board and a string word, return true if word exists in the grid.
44

5-
// The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
6-
7-
5+
// The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or
6+
// vertically neighboring. The same letter cell may not be used more than once.
87

98
// Example 1:
10-
11-
129
// Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
1310
// Output: true
1411
// Example 2:
15-
16-
1712
// Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
1813
// Output: true
1914

84.LargestRectangleArea.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 84. Largest Rectangle in Histogram
2-
// Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
2+
// Given an array of integers heights representing the histogram's bar height where the width of each bar is 1,
3+
// return the area of the largest rectangle in the histogram.
34
// Example 1:
45
// Input: heights = [2,1,5,6,2,3]
56
// Output: 10

88.Merge.cs

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// 88. Merge Sorted Array
2-
// You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
2+
// You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n,
3+
// representing the number of elements in nums1 and nums2 respectively.
34
// Merge nums1 and nums2 into a single array sorted in non-decreasing order.
45
// The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
56

7+
//Time O(nlogn)
68
public class Solution
79
{
810
public void Merge(int[] nums1, int m, int[] nums2, int n)
@@ -13,4 +15,43 @@ public void Merge(int[] nums1, int m, int[] nums2, int n)
1315
}
1416
Array.Sort(nums1);
1517
}
16-
}
18+
}
19+
20+
//Space O(n+m) Time O(n+m)
21+
public class Solution {
22+
public void Merge(int[] nums1, int m, int[] nums2, int n) {
23+
int[] res = new int[m+n];
24+
int i = 0, j = 0, k = 0;
25+
while( i < m && j < n){
26+
if(nums1[i] <= nums2[j])
27+
res[k++] = nums1[i++];
28+
else
29+
res[k++] = nums2[j++];
30+
}
31+
while( i < m){
32+
res[k++] = nums1[i++];
33+
}
34+
while(j < n){
35+
res[k++] = nums2[j++];
36+
}
37+
res.CopyTo(nums1,0);
38+
}
39+
}
40+
//Time O(n,m) Space O(1)
41+
public void Merge(int[] nums1, int m, int[] nums2, int n) {
42+
int i = m - 1;
43+
int j = n - 1;
44+
int k = m + n - 1;
45+
46+
while(i >= 0 && j >= 0) {
47+
if(nums1[i] > nums2[j]) {
48+
nums1[k--] = nums1[i--];
49+
} else {
50+
nums1[k--] = nums2[j--];
51+
}
52+
}
53+
54+
while(j >= 0) {
55+
nums1[k--] = nums2[j--];
56+
}
57+
}

0 commit comments

Comments
 (0)