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 climbStairs(int n) { | |
if (n < 2) | |
return n; | |
int[] numOnes = new int[n]; | |
int[] numTwos = new int[n]; | |
numOnes[0] = 1; | |
numTwos[0] = 0; | |
int[] dp = new int[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
public class Solution { | |
public int atoi(String str) { | |
str = str.trim(); | |
int len = str.length(); | |
char[] s = str.toCharArray(); | |
if (len == 0) return 0; | |
int i = 0; | |
boolean isNeg = false, invalid = false; | |
if (s[0] == '-') { | |
isNeg = true; |
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 CountingSort { | |
public void sortColors(int[] A) { | |
int len = A.length; | |
if (len < 2) return; | |
int zeros = 0, ones = 0, twos = 0, i = 0; | |
for (i = 0; i < len; i++) { | |
if (A[i] == 0) | |
zeros++; | |
else if (A[i] == 1) | |
ones++; |
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[] twoSum(int[] numbers, int target) { | |
int len = numbers.length; | |
int[] result = new int[2]; | |
HashMap<Integer, Integer> map = new HashMap<Integer,Integer>(); | |
result[0] = result[1] = -1; | |
if (len < 2) return result; | |
int num = 0; | |
for (int i = 0; i < len; i++) { | |
num = numbers[i]; |
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
// you can also use imports, for example: | |
// import java.math.*; | |
// you can use System.out.println for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
// Time Complexity: O(N) | |
// Space Complexity: O(1) | |
class Solution { | |
public int solution(String S) { |
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
import java.util.Stack; | |
class Solution { | |
public int solution(String S) { | |
// write your code in Java SE 8 | |
int len = S.length(); | |
if (len == 0) | |
return 1; | |
if (len == 1) | |
return 0; | |
char ch; |
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
// you can also use imports, for example: | |
// import java.math.*; | |
// you can use System.out.println for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
int len = A.length; |
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
// you can also use imports, for example: | |
// import java.math.*; | |
// you can use System.out.println for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
import java.util.Arrays; | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
int len = A.length; |
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
import java.util.Arrays; | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
Arrays.sort(A); | |
int len = A.length; | |
int right = A[len-1]*A[len-2]*A[len-3]; | |
int left = A[0]*A[1]*A[len-1]; | |
return right > left ? right : 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
// you can also use imports, for example: | |
// import java.math.*; | |
// you can use System.out.println for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
import java.util.HashSet; | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
int len = A.length; |
NewerOlder