Skip to content

Instantly share code, notes, and snippets.

View wangchauyan's full-sized avatar
🎯
Focusing

Chauyan wangchauyan

🎯
Focusing
View GitHub Profile
@wangchauyan
wangchauyan / PlusOne
Created November 17, 2014 01:43
LeetCode - Plus one
public class Solution {
public int[] plusOne(int[] array) {
if (array == null) return null;
int carry = 0;
for(int i = array.length-1; i >= 0; i--) {
array[i] += (i == array.length-1 ? 1+carry : carry);
carry = array[i]/10;
array[i] %= 10;
}
if(carry == 0) return array;
/*
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
*/
@wangchauyan
wangchauyan / longestSubString
Created December 1, 2014 10:58
Longest sub string in a string - LeetCode
/*
Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "abcabcbb" is "abc",
which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
*/
public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0) return 0;
// Time Complexity = O(n*2) = O(n), Space Complexity = O(n)
@wangchauyan
wangchauyan / Level Order Traversal
Created December 10, 2014 09:08
Tree Level Order Traversal
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@wangchauyan
wangchauyan / isBalanceTree
Created December 19, 2014 14:24
Balance Tree Check
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
if(checkHeight(root) == -1) return false;
else return true;
}
int checkHeight(TreeNode node) {
if(node == null) return 0;
int leftHeight = checkHeight(node.left);
@wangchauyan
wangchauyan / string2Int.java
Created December 19, 2014 15:39
String to Integer
package wcy.leetcode.gist;
/**
* Created by ChauyanWang on 12/19/14.
*/
public class string2Int {
public int atoi(String number) {
if (number == null || number.length() == 0) return 0;
double result = 0;
@wangchauyan
wangchauyan / romanToInt.java
Created December 19, 2014 17:12
Roman String to Integer
package wcy.leetcode.gist;
/**
* Created by ChauyanWang on 12/20/14.
*/
public class romanToInt {
int getInteger(char c) {
switch (c) {
case 'i': return 1;
case 'v': return 5;
@wangchauyan
wangchauyan / Longest_Common_Prefix.java
Created December 20, 2014 07:29
Find out longest common prefix
package wcy.leetcode.gist;
/**
* Created by ChauyanWang on 12/20/14.
*/
public class Longest_Common_Prefix {
int findMinLength(String[] strs) {
int minLength = Integer.MAX_VALUE;
for(String str : strs) {
@wangchauyan
wangchauyan / Valid_Palindrome.java
Created December 20, 2014 07:50
Valid a string is palindrome or not.
package wcy.leetcode.gist;
/**
* Created by ChauyanWang on 12/20/14.
*/
public class Valid_Palindrome {
public boolean Solution (String s) {
if( s == null || s.length() == 0) return true;
s = s.toLowerCase();
int start = 0;
@wangchauyan
wangchauyan / Path_Sum.java
Created December 20, 2014 09:23
Find a path through root to leaf which's value is equal to specific sum value. And then return true or fasle.
package wcy.leetcode.gist;
import java.util.LinkedList;
/**
* Created by ChauyanWang on 12/20/14.
*/
public class Path_Sum {
class TreeNode {
TreeNode left;