Coding Easy Flashcards

1
Q

Reverse String

A

x/10 gives denominator - remainder
x%10 gives reminder - last digit

class Solution {
    public int reverse(int x) {
        int reverse = 0;
        int lastDigit = 0;
        while(x != 0) {
            if (reverse < Integer.MIN_VALUE/10 || reverse > Integer.MAX_VALUE/10) return 0;
            lastDigit = x % 10;
            reverse = reverse * 10 + lastDigit;
            x = x/10;
        }
    return reverse;
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Roman to Integer

A
class Solution {
    public int romanToInt(String s) {
        int total = 0;
        Map store = Map.of(
            'I', 1,
            'V', 5,
            'X', 10,
            'L', 50,
            'C', 100,
            'D', 500,
            'M', 1000
        );
    for (int i = 0, j = 1; i < s.length(); i++, j++) {
        switch(s.charAt(i)) {
            case 'I': if (j
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Longest common prefix

A

Vertical Scanning + Edge Case: Pick the smallest string in order to compare against, otherwise index out of bounds

class Solution {
    public String longestCommonPrefix(String[] strs) {
        String output = "";
        int min_len = Integer.MAX_VALUE;
        int min_index = 0;
    for (int i=0; istrs[i].length()) { min_len = strs[i].length();  min_index = i;}
    }

     for (int j=0;j
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Parenthesis

A

Stack
Store the close parenthesis keys in map too because ][ is a valid input

class Solution {
    public boolean isValid(String s) {
        Map store = Map.of('(', ')', '{', '}', '[', ']', ')', 'a', '}', 'a', ']', 'a');
    Stack parenthesis = new Stack();

    for (int i = 0; i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Merge two sorted lists

A

Time limit exceeded

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = null;
        ListNode prev = new ListNode();
        ListNode curr = null;
        while (l1.next != null || l2.next != null) { 
            if (l1.val <= l2.val) {  
                curr  = new ListNode(l1.val, null);    
            } else {
                curr  = new ListNode(l2.val, null); 
            }
            prev.next = curr;
            if (head == null) head = curr; 
            prev = curr; 
        }
        return head; 
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Remove duplicates from sorted array

A
class Solution {
    public int removeDuplicates(int[] nums) {
        int res = -1;
        if (nums.length == 0 || nums.length == 1) return nums.length;
        int j = 1;
        for (int i = 0; i nums.length - 1) {
                res = i+1;
                break;
            }
        }
        return res;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

strStr() first occurrence of substring

A

if (needle.isEmpty()) return 0;
int res = -1;
for (int i = 0, j = 0; i < haystack.length(); i++) {
while (j < needle.length() && haystack.charAt(i) == needle.charAt(j)) {
if (res == -1) res = i;
i++;
j++;
}
if (j == needle.length() ) return i; else { i = res + 1; j = 0; res = -1;}
}
return res;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

PlusOne

A

Find if there is overflow - create a new array

class Solution {
    public int[] plusOne(int[] digits) {
      int j = digits.length -1;
      int i = j;
      int[] outputArray;
      if (digits[i] != 9) { 
        digits[i]++;
        return digits;
      } else {
          while(j>=0 && digits[j] == 9) {
              digits[j] = 0;
              j--;
          }
          if (j == -1) {
            outputArray = new int[digits.length + 1];
            outputArray[0] = 1;
            System.arraycopy(digits, 0, outputArray, 1, digits.length);
            return outputArray;
          } else {
              digits[j]++;
          }
      }
      return digits;
    }      
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

ClimbStairs

A

You might take 1 step or two
Memoization must be initiated outside recursive method
By default array will be filled with zero

class Solution {
    int[] memo = new int[46];
    public int climbStairs(int n) {
        if(n == 0) return 1;
        if(n < 0) return 0;
    if (memo[n] != 0) return memo[n];

    int res = climbStairs(n-1)+climbStairs(n-2);
    return memo[n] = res;    
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly