Skip to content

Instantly share code, notes, and snippets.

@jaredrummler
Last active June 25, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredrummler/41594a159d8b4fb70efa04ed1873dc6b to your computer and use it in GitHub Desktop.
Save jaredrummler/41594a159d8b4fb70efa04ed1873dc6b to your computer and use it in GitHub Desktop.
I don't even have any good coding skillz. You know like algorithmic puzzle skills, converting C to x86 assembly using a rock and chisel skills, designing data structures for a 10^10x10^10 Tic-Tac-Toe game skills. Companies only want software engineers who have great skills!

A few leet code like questions/answers from years ago. They aren't optimized but it's my code. I'm a work-in-progress 🙃. Wanted to save some of those old gists and hopefully will add to it in the future.

Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        for (ListNode ln = l1; ln != null; ln = ln.next) {
          s1.insert(0, ln.val);
        }

        for (ListNode ln = l2; ln != null; ln = ln.next) {
          s2.insert(0, ln.val);
        }

        int n1 = Integer.parseInt(s1.toString());
        int n2 = Integer.parseInt(s2.toString());

        String string = new StringBuilder(String.valueOf(n1 + n2)).reverse().toString();

        ListNode result = new ListNode(0);
        ListNode temp = result;
        for (int i = 0; i < string.length(); i++) {
          temp.next = new ListNode(Integer.parseInt(String.valueOf(string.charAt(i))));
          temp = temp.next;
        }

        return result.next;
    }
}

Java:

public <T> ListNode<T> reverse(ListNode<T> nodes) {
  if (nodes == null || nodes.next == null) return nodes;

  List<T> list = new ArrayList<>();
  for (ListNode<T> ln = nodes; ln != null; ln = ln.next) {
    list.add(ln.value);
  }

  Collections.reverse(list);
  ListNode<T> result = new ListNode<>(null);
  ListNode<T> temp = result;
  for (T t : list) {
    temp.next = new ListNode<>(t);
    temp = temp.next;
  }

  return result.next;
}

Java:

boolean sudoku2(char[][] grid) {
  Set<Character> set = new HashSet<>();
  int length = grid.length;

  for (char[] row : grid) {
    for (char c : row) {
      if (Character.isDigit(c) && set.contains(c)) {
        return false;
      }
      set.add(c);
    }
    set.clear();
  }

  for (int i = 0; i < length; i++) {
    for (char[] row : grid) {
      char c = row[i];
      if (Character.isDigit(c) && set.contains(c)) {
        return false;
      }
      set.add(c);
    }
    set.clear();
  }

  for (int i = 0; i < length; i += 3) {
    for (int j = 0; j < length; j += 3) {
      for (int k = i; k < i + 3; k++) {
        for (int l = j; l < j + 3; l++) {
          char c = grid[k][l];
          if (Character.isDigit(c) && set.contains(c)) {
            return false;
          }
          set.add(c);
        }
      }
      set.clear();
    }
  }

  return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment