This file contains hidden or 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 { | |
// 从后往前倒序merge | |
public void merge(int[] nums1, int m, int[] nums2, int n) { | |
int index = m + n; | |
while(m > 0 && n > 0) { | |
if(nums1[m-1] >= nums2[n-1]) { | |
nums1[--index] = nums1[--m]; | |
} else { | |
nums1[--index] = nums2[--n]; |
This file contains hidden or 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 { | |
// O(n) time, O(n) space | |
public int[] twoSum(int[] nums, int target) { | |
int[] result = new int[2]; | |
if(nums == null || nums.length < 2) { | |
return result; | |
} | |
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); | |
for(int i=0; i<nums.length; i++) { |
This file contains hidden or 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
/** | |
* Definition for singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
// create a fake node first and then add two sorted list to the new list one by one |
This file contains hidden or 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
// first, if str is null or white space | |
// second, sign is positive or negative | |
// third, compute result: no other letter than '0'-'9' and within Integer range | |
// fourth, make sure the range is between max and min Integer, pay attention to overflow! | |
public class Solution { | |
public int myAtoi(String str) { | |
if(str == null || str.length() == 0) { | |
return 0; | |
} | |
str = str.trim(); |
This file contains hidden or 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 { | |
// new idea | |
public boolean isValid(String s) { | |
if(s == null || s.length() == 0) { | |
return false; | |
} | |
Stack<Character> stack = new Stack<Character>(); | |
int len = s.length(); | |
for(int i=0; i<len; i++) { |
This file contains hidden or 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 { | |
// We start with trimming. | |
// If we see [0-9] we reset the number flags. | |
// We can only see . if we didn't see e or .. | |
// We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag. | |
// We can only see + and - in the beginning and after an e | |
// any other character break the validation. | |
// At the and it is only valid if there was at least 1 number and if we did see an e then a number after it as well. |
This file contains hidden or 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 boolean isPalindrome(String s) { | |
if(s == null) { | |
return false; | |
} | |
String str = s.trim().toLowerCase(); | |
int start = 0; | |
int end = str.length() - 1; | |
while(start < end) { | |
char c1 = str.charAt(start); |
This file contains hidden or 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 { | |
// method 1: fibonnaci f(n) = f(n-1) + f(n-2), 意思是当前的ways的数量是前一步ways数量以及前两步ways数量的和 | |
// time O(n) space O(1) | |
public int climbStairs(int n) { | |
if(n < 2) return n; | |
int prev = 1; | |
int cur = 1; | |
for(int i=2; i<=n; i++) { | |
int update = cur + prev; |
This file contains hidden or 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 { | |
// Two Pointers, O(n^2) | |
public ArrayList<ArrayList<Integer>> threeSum(int[] nums) { | |
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
if(nums == null || nums.length < 3) { | |
return result; | |
} | |
Arrays.sort(nums); | |
for(int i=0; i<nums.length-2; i++) { |
This file contains hidden or 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 { | |
// solution 1: use constant space | |
// 思路:把所有0都放在第一行或第一列 | |
public void setZeroes(int[][] matrix) { | |
int m = matrix.length; | |
int n = matrix[0].length; | |
if(m == 0 || n ==0) return; | |
boolean hasZeroFirstRow = false; |
OlderNewer