Skip to content

Instantly share code, notes, and snippets.

View Jeffwan's full-sized avatar

Jiaxin Shan Jeffwan

  • Bytedance
  • Seattle, WA
View GitHub Profile
@Jeffwan
Jeffwan / ReverseString.java
Created February 7, 2014 04:20
Implement a function reverse which reverses a string. CC 150 - Arrays and Strings
package CC150.ArraysAndString;
/**
* Implement a function reverse which reverse a null-terminated string (null-terminated only works for C/C++)
*/
public class ReverseString {
public static void main(String args[]) {
System.out.println(reverse("hello"));
}
@Jeffwan
Jeffwan / Permutation.java
Created February 7, 2014 05:17
Given two strings, write a method to decide if one is a permutation of the other. CC150 - Arrays and Strings.
package CC150.ArraysAndString;
/**
* I misunderstand the defination of permutation. I treat it as reverse.
* Permutation means anyway, but reversed. abb is a permutation of bab.
* Permutation feature: the count of ever number is same, any combination is valid
*/
public class Permutation {
public static void main(String[] args) {
System.out.println(permutation("fabcdfedfaaa","abacdaedfaff"));
@Jeffwan
Jeffwan / ReplaceSpaces.java
Created February 7, 2014 06:06
Replace Spaces. Input "Mr John Smith " Output "Mr%20John%Smith". CC150 - Arrays and Strings
package CC150.ArraysAndString;
/**
* 1. Calculate spaces count first, then we know new char[] length
* 2. To prevent overlap, start replace from end to head
*
* true length < str.length because of input may end with many spaces.
* length = str.length - end spaces
*
* book use newLength - 1, then newLength--, I use step here.
@Jeffwan
Jeffwan / Compression.java
Created February 7, 2014 20:01
Basic String Compression Input: aabbc Output: a2b2c1 CC 150 - Arrays and Strings
package leetcode;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
@Jeffwan
Jeffwan / IsRotation.java
Created February 7, 2014 20:09
Check if s2 is a rotation of s1 using only one call to isSubstring. CC150 - Arrays and Strings
package CC150.ArraysAndString;
/**
* Check if s2 is a rotation of s1 using only one call to isSubstring
* eg: "waterbottle" is a rotation of "erbottlewat"
*
*/
public class IsRotation {
public boolean isRotation2(String s1, String s2) {
@Jeffwan
Jeffwan / Combine.java
Last active August 29, 2015 13:56
Combinations @leetcode
package leetcode.combinations;
import java.util.ArrayList;
public class Combine {
public static void main(String args[]) {
System.out.println(combine(4,2));
}
/**
@Jeffwan
Jeffwan / ReverseInteger.java
Last active August 29, 2015 13:56
Reverse digits of an integer. @leetcode
package leetcode.simpleCoding;
/**
* @author jeffwan
* @date Feb 8, 2014
*
* Objective: write the simple and readable codes
* There's no algorithm in this kind of problem but some tiny simple method. Our goal is to make it as clear as possible.
* In addition, It's a good opportunity communicatting with interviewer about the overflow, 100 -- 001 (display).
*
@Jeffwan
Jeffwan / StrStr.java
Last active August 29, 2015 13:56
Implement strStr() @leetcode
package leetcode.simpleCoding;
/**
* @date Feb 8, 2014
*
* Test case: (1123 123) ("","") --> may OutOfBound ,("a","a") --> s1.length()- s2.length()+1 not without +1, bound check.
* return value: haystack.subString(i) but not String.valueOf(i) ("","" will return 0).
* take care
*/
public class StrStr {
@Jeffwan
Jeffwan / InorderTraversal.java
Created February 8, 2014 22:51
Binary Tree Inorder Traversal @leetcode
package leetcode.tree;
import java.util.ArrayList;
import java.util.Stack;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
@Jeffwan
Jeffwan / PreorderTraversal.java
Created February 8, 2014 22:52
Binary Tree Preorder Traversal @leetcode
package leetcode.tree;
import java.util.ArrayList;
import java.util.Stack;
import leetcode.tree.InorderTraversal.TreeNode;
public class PreorderTraversal {
public ArrayList<Integer> preorderTraversal(TreeNode root) {