Skip to content

Instantly share code, notes, and snippets.

View hsaputra's full-sized avatar

Henry Saputra hsaputra

View GitHub Profile
class Solution {
public int[] twoSum(int[] nums, int target) {
// Tests:
// 1. Empty array
// 2. 1 element
// 3. Negative vs positive
// Exactly One solution
if (nums == null || nums.length == 0) {
return null;
class Solution {
public int lengthOfLongestSubstring(String s) {
// check for invalid arguments.
if (s == null || s.length() == 0) {
return 0;
}
if (s.length() == 1) {
return 1;
}
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs == null || strs.length == 0)
return new LinkedList<List<String>>();
Map<String, List<String>> superAnagrams = new HashMap<>();
int sLen = strs.length;
for (String str : strs) {
class Solution {
public int mySqrt(int x) {
if (x == 1 || x == 0) {
return x;
}
long low = 1;
long high = x;
// Need to use binary search to fix y such that y*y = x
/**
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
board =
[
['A','B','C','E'],
class Solution {
public List<List<Integer>> subsets(int[] nums) {
// check
if (nums == null) {
throw new IllegalArgumentException();
}
List<List<Integer>> results = new LinkedList<List<Integer>>();
// prepare to recursive
class Solution {
public void sortColors(int[] nums) {
// nums value is 0, 1, 2
int left = 0;
int right = nums.length-1 ;
int i = 0;
// move 0s and 2s so 1s will settle by themselves
class Solution {
private int lo, maxLen;
public String longestPalindrome(String s) {
int len = s.length();
if (len < 2)
return s;
for (int i = 0; i < len-1; i++) {
extendPalindrome(s, i, i); //assume odd length, try to extend Palindrome as possible
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
return results;
}
List<Integer> prefix = new ArrayList<Integer>();
permutateRecursive(nums, prefix, results);
class Solution {
public int uniquePaths(int m, int n) {
// m is columns
// n is rows
if (m < 1 || n < 1) {
return 0;
}
if (m == 1 && m == 1) {