This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wcy.leetcode.gist; | |
import java.util.LinkedList; | |
/** | |
* Created by ChauyanWang on 12/20/14. | |
*/ | |
public class Path_Sum { | |
class TreeNode { | |
TreeNode left; |
OlderNewer