Skip to content

Instantly share code, notes, and snippets.

View Siyu-Lei's full-sized avatar

Siyu Lei Siyu-Lei

View GitHub Profile
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];
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++) {
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) {
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;
}
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]) {
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++;
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));
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;
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);
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) {