#####################################################
#nums so far : 49
#rule: no solution peek, if so:
blog, mark, re-check afterwards
only blog can't-dos
#####################################################
1 Remove Duplicates from Sorted Array
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if(n == 0 || n == 1) return n;
int i = 0, j = 1;
while(j < n){
if(nums[i] != nums[j]){
swap(nums, i + 1, j);
i++;
}
j++;
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
2 Move Zeros
simply consider different scenario of starting condition (i == j / i != j) and swap condition (swap(i,j) / swap(i+1, j)) see if the problem solved.
class Solution {
public void moveZeroes(int[] nums) {
int i = 0, j = i, n = nums.length;
while(j < n){
if(nums[j] != 0) swap(nums, i++, j++);
else j++;
}
}
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
14 Longest Common Prefix
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
String ans = "";
int k = 0;
for(int i = 0; i < strs[0].length(); i++){
char firstPre = strs[0].charAt(i);
for(int j = 1; j < strs.length; j++){
if(i >= strs[j].length() || strs[j].charAt(i) != firstPre) return ans;
}
ans += firstPre;
}
return ans;
}
}
19 Remove Nth Node From End of List
class Solution{
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode i = head, j = head;
while(n-- > 0) j = j.next;
if(j == null){ // can only be for m-length listnode, asking to delete the mth from //the back, which is the first one
head = head.next;
return head;
}
while(j.next != null){
i = i.next;
j = j.next;
}
//make i is the one before the one to be deleted
i.next = i.next.next;
return head;
// if make (j != null) then
// i.val = i.next.val;
// i.next = i.next.next;
//won't work for the val could not exist
}
}
206 Reverse Linked List (both recursive and iterative)
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode NewHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return NewHead;
}
}
//
//
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode prev = null, cur = head;
while(cur != null){
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
}
98 Validate Binary Search Tree
class Solution{
public boolean isValidBST(TreeNode root){
return isValid(root, null, null);
}
public boolean isValid(TreeNode root, Integer upper, Integer lower){
if(root == null) return true;
int val = root.val;
if((upper != null && val >= upper) || (lower != null && val <= lower)) return false;
return isValid(root.left, val, lower) && isValid(root.right, upper, val);
}
}
102 Binary Tree Level Order Traversal
//iterativeSolution
class Solution {
public List<List<Integer>> levelOrder(TreeNode root){
List<List<Integer>> ans = new ArrayList<>();
if(root == null) return ans;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
List<Integer> curlevel = new ArrayList<>();
int num = q.size();//actually the same!!!
for(int i = 0; i < num; i++){
TreeNode node = q.remove();
curlevel.add(node.val);
if(node.left != null) q.add(node.left);
if(node.right != null) q.add(node.right);
}
ans.add(curlevel);
}
return ans;
}
}
//recursively
class Solution {
List<List<Integer>> levels = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root){
if(root == null) return levels;
bfs(root, 0);
return levels;
}
public void bfs(TreeNode root, int level){
if(level == levels.size()) levels.add(new ArrayList<Integer>());
levels.get(level).add(root.val);
if(root.right != null) bfs(root.right, level + 1);
if(root.left != null) bfs(root.left, level + 1);
}
}
108 Convert Sorted Array to Binary Search Tree
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return dfs(nums, 0, nums.length);
}
public TreeNode dfs(int[] nums, int l, int r){
if(l == r) return null;
int mid = l + (r - l) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = dfs(nums, l, mid);
root.right = dfs(nums, mid + 1, r);
}
}
}
53 Maximum Subarray
//1. divide and conquer · define base case : n == 1, max is the only element · divide into sub problems, solve them recursively (with base case) · merge sub solutions and return general solution
class Solution{
public int maxSubArray(int[] nums){
if(nums.length == 0) return Integer.MIN_VALUE;
return recursiveMax(nums, 0, nums.length - 1);
}
public int recursiveMax(int[] nums, int start, int end){
if(start == end) return nums[start];
int mid = start + (end - start) / 2;
int left_sum = recursiveMax(nums, start, mid);
int right_sum = recursiveMax(nums, mid + 1, end);
int cross_sum = crossSum(nums, start, end, mid);
return Math.max(Math.max(left_sum, right_sum), cross_sum);
}
public int crossSum(int[] nums, int s, int e, int p){
//maximum of the cross sum is the left max + right max
int curmax = 0, leftmax = Integer.MIN_VALUE;
for(int i = p; i >= s; i--){
curmax += nums[i];
leftmax = Math.max(leftmax, curmax);
}
curmax = 0;
int rightmax = Integer.MIN_VALUE;
for(int i = p + 1; i <= e; i++){
curmax += nums[i];
rightmax = Math.max(curmax, rightmax);
}
return leftmax + rightmax;
}
}
//2. greedy: each step to do local optimal, and get the global optimal
class Solution{
public int maxSubArray(int[] nums){
if(nums.length == 0) return Integer.MIN_VALUE;
int curr = nums[0], global = nums[0];
for(int i = 1; i < nums.length; i++){
curr = Math.max(nums[i], nums[i] + curr);
global = Math.max(curr, global);
}
return global;
}
}
384 Shuffle an Array
//know how to use random.nextInt(int n)
//nextInt(n) generate a random int between 0 (included) and n (not included)
204 Count Primes
class Solution{
public int CountPrimes(int n){
boolean[] isPrimes = new boolean[n + 1];
Arrays.fill(isPrimes, true);
for(int i = 2; i < Math.sqrt(n); i++){
if(isPrimes[i]){
for(int j = i * i; j < n; j += i) isPrimes[j] = false;
}
}
int ans = 0;
for(int i = 2; i < n; i++) ans = isPrimes[i] ? ans + 1 : ans;
return ans;
}
}