This file contains hidden or 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 hidden or 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 void rotate(int[][] matrix) { | |
int n = matrix.length-1; | |
if (n == 0) return; | |
for (int i = 0; i <= n/2; i++){ | |
for (int j = i; j < n-i; j++){ | |
int temp = matrix[i][j]; | |
matrix[i][j] = matrix[n-j][i]; | |
matrix[n-j][i] = matrix[n-i][n-j]; |
This file contains hidden or 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 hidden or 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 List<List<Integer>> generate(int numRows) { | |
List<List<Integer>> allRows = new ArrayList<List<Integer>>(); | |
ArrayList<Integer> row = new ArrayList<Integer>(); | |
for (int i = 0; i < numRows; i++){ | |
row.add(0, 1); | |
for (int j = 1; j < row.size()-1; j++){ | |
row.set(j, row.get(j) + row.get(j+1)); | |
} | |
allRows.add(new ArrayList<Integer>(row)); |
This file contains hidden or 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 maxSubArray(int[] A) { | |
int maxSoFar = A[0]; | |
int sumSoFar = A[0]; | |
for (int i = 1; i < A.length; i++){ | |
sumSoFar = sumSoFar + A[i]; | |
sumSoFar = Math.max(sumSoFar, A[i]); | |
maxSoFar = Math.max(sumSoFar, maxSoFar); | |
} | |
return maxSoFar; |
This file contains hidden or 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 longestValidParentheses(String s) { | |
Stack<Integer> stk = new Stack<Integer>(); | |
int left = -1; | |
int maxCount = 0; | |
for (int i = 0; i < s.length(); i++){ | |
if (s.charAt(i) == '('){ | |
stk.push(i); | |
} | |
else{ |
This file contains hidden or 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 String largestNumber(int[] num) { | |
String[] arr = new String[num.length]; | |
for (int i = 0; i < num.length; i++){ | |
arr[i] = Integer.toString(num[i]); | |
} | |
Comparator<String> comp = new Comparator<String>(){ |
This file contains hidden or 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
/* | |
Question: Given a sequence of positive integers A and an integer T, return whether there is a continuous sequence of A that sums up to exactly T | |
*/ | |
public class SequenceSum | |
{ | |
public static void main(String[] args) | |
{ |
This file contains hidden or 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
//This is the brute force implementation | |
import java.util.HashSet; | |
public class MatchesWithStars | |
{ | |
public static void main(String[] args) | |
{ | |
HashSet<String> words = new HashSet<String>(); | |
words.add("hello"); |
This file contains hidden or 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.HashSet; | |
import java.util.HashMap; | |
public class MinUnique | |
{ | |
public static void main(String[] args) | |
{ | |
String str = "cabbcbcbbccaa"; | |
HashSet<Character> set = new HashSet<Character>(); | |
set.add('a'); |