Arrays Flashcards

1
Q

Move Zeroes

A
public class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0;
        int j = 0;
        while (i < nums.length){
            if (nums[i] != 0){
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                j++;
            }
            i++;
        }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Merge Two Sorted Arrays

A
public class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int index = m + n - 1;
        int i = m-1;
        int j = n-1;
        while (i >= 0 && j >= 0){
          if (nums1[i] > nums2[j]) nums1[index--] = nums1[i--];
          else nums1[index--] = nums2[j--];
        }
    while (i >= 0) nums1[index--] = nums1[i--];
    while (j >= 0) nums1[index--] = nums2[j--];
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Buy and Sell Stocks

A
public class Solution {
    public int maxProfit(int[] prices) {
        int profit = 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sort Color

A
class Solution {
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0) return;
    int left = 0;
    int right = nums.length - 1;
        int i = 0;
        while (i <= right){
          if (nums[i] == 0){
            swap(nums, i, left);
            left++;
            i++;
          }
          else if(nums[i] == 1) i++;
          else{
            swap(nums, i, right);
            right--;
          }
        }
    }
    public void swap(int[] nums, int a, int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Sort color II

A
class Solution {
    public void sortColors2(int[] colors, int k) {
        int low = 1, high = k, left = 0, right = colors.length-1;
        while(low < high){
            int i = left;
            while(i <= right){
                if(colors[i] == low){
                    swap(colors, i, left);
                    left++;
                    i++;
                }
                else if(colors[i] == high){
                    swap(colors, i, right);
                    right--;
                }
                else{
                    i++;
                }
            }
            low++;
            high--;
        }
    }
    private void swap(int[] colors, int i, int j){
        int temp = colors[i];
        colors[i] = colors[j];
        colors[j] = temp;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Random Pick Index

A
class Solution {
    int[] nums;
    Random rand;
    public Solution(int[] nums) {
        this.nums = nums;
        this.rand = new Random();
    }
public int pick(int target) {
    int count = 0;
    int result = 0;
    for (int i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

H Index

A
class Solution {
    public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int len = citations.length;
        int j = 0;
        for (int i = len-1;i>=0;i--){
            j = len-i-1;
            if (j >= citations[i]) return j;
        }
        return len;
    }
}
class Solution{
   public int hIndex(int[] citations) {
     int len = citations.length;
     int left = 0;
     int right = len-1;
     while (left <= right){
      int mid = left + (right - left) / 2;
      if (citations[mid] < len - mid)
        left = mid + 1;
      else if(citations[mid] >= len - mid)
        right = mid - 1;
     }
     return len - left; 
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Remove Duplicates from Sorted Array I & II

A
public class Solution {
    public int removeDuplicates(int[] nums) {
      if(nums == null || nums.length == 0) return 0;
      int len = nums.length;
      int j = 0;
      for (int i = 1; i< len;i++)
        if (nums[i] != nums[j]) nums[++j] = nums[i];
      return j + 1;
    }
}
class Solution {
    public int removeDuplicates(int[] nums) {
        int i = 0;
        for(int n : nums)
            if(i < 1 || n > nums[i - 1])  nums[i++] = n;
        return i;
    }
}
class Solution {
    public int removeDuplicates(int[] nums) {
        int i = 0;
        for(int n : nums)
            if(i < 2 || n > nums[i - 2])  nums[i++] = n;
        return i;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly