This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] plusOne(int[] digits) { | |
for (int i = digits.length - 1; i >= 0; i--) { | |
if (digits[i] < 9) { | |
digits[i]++; | |
return digits; | |
} | |
digits[i] = 0; | |
} | |
int[] res = new int[digits.length + 1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public void setZeroes(int[][] matrix) { | |
int col0 = 1; | |
int row = matrix.length; | |
int col = matrix[0].length; | |
for (int i = 0; i < row; i++) { | |
if (matrix[i][0] == 0) { | |
col0 = 0; | |
} | |
for (int j = 1; j < col; j++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public void sortColors(int[] nums) { | |
int redPos = 0; | |
int bluePos = nums.length - 1; | |
int index = 0; | |
while (index <= bluePos) { | |
if (nums[index] == 0) { | |
nums[index++] = nums[redPos]; | |
nums[redPos++] = 0; | |
} else if (nums[index] == 2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int removeDuplicates(int[] nums) { | |
int i = 0; | |
for (int n = 0; n < nums.length; n++) { | |
if(i < 2 || nums[n] > nums[i - 2]) { | |
nums[i++] = nums[n]; | |
} | |
} | |
return i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean checkPossibility(int[] nums) { | |
int count = 0; | |
for (int i = 1; i < nums.length; i++) { | |
if (nums[i - 1] > nums[i]) { | |
count++; | |
if (count > 1) { | |
return false; | |
} | |
if (i < 2 || nums[i - 2] <= nums[i]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxArea(int[] height) { | |
int start = 0; | |
int end = height.length - 1; | |
int max = 0; | |
while (start < end) { | |
int min = Math.min(height[start], height[end]); | |
max = Math.max(min * (end - start), max); | |
while (height[start] <= min && start < end) { | |
start++; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> generate(int numRows) { | |
List<List<Integer>> res = new ArrayList(); | |
List<Integer> row = new ArrayList(); | |
for (int i = 0; i < numRows; i++) { | |
row.add(0, 1); | |
for (int j = 1; j < row.size() - 1; j++) { | |
row.set(j, row.get(j) + row.get(j + 1)); | |
} | |
res.add(new ArrayList(row)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int findMin(int[] nums) { | |
int left = 0; | |
int right = nums.length - 1; | |
while (left < right) { | |
int mid = left + (right - left) / 2; | |
if (nums[mid] <= nums[right]) { | |
right = mid; | |
} else { | |
left = mid + 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int divide(int dividend, int divisor) { | |
// according to note: | |
// For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows. | |
if (dividend == Integer.MIN_VALUE && divisor == -1) { | |
return Integer.MAX_VALUE; | |
} | |
boolean sign = (dividend > 0) ^ (divisor > 0); | |
long dvd = Math.abs((long) dividend); | |
long dvs = Math.abs((long) divisor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int findDuplicate(int[] nums) { | |
int slow = nums[0]; | |
int fast = nums[nums[0]]; | |
while (slow != fast) { | |
slow = nums[slow]; | |
fast = nums[nums[fast]]; | |
} | |
fast = 0; | |
while (slow != fast) { |
OlderNewer