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.lang.Math; | |
public class Solution { | |
public int maxProfit(int[] prices) { | |
if (prices.length <= 1) return 0; | |
int minPrice = prices[0]; | |
int maxSoFar = Integer.MIN_VALUE; | |
int profitSoFar = Integer.MIN_VALUE; |
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
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
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 int reverse(int x) { | |
long rev = 0; | |
while(x != 0){ | |
rev = rev*10 + x%10; | |
x = x/10; | |
} | |
return (int)rev; | |
} |
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 MainClass { | |
public static void main(String[] args) { | |
int nDisks = 3; | |
doTowers(nDisks, 1, 2, 3); | |
} | |
public static void doTowers(int topN, int from, int inter, int to) { | |
if (topN == 1){ | |
System.out.println("Disk 1 from " + from + " to " + to); | |
}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
void mergeSort(int[] array){ | |
int[] helper = new int[array.length]; | |
mergeSort(array, helper, 0, array.length - 1); | |
} | |
void mergeSort(int[] array, int[] helper, int low, int high){ | |
if (low < high){ | |
int middle = (low + high) / 2; | |
mergeSort(array, helper, low, middle); | |
mergeSort(array, helper, middle + 1, high); |
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 void swap(int[] A, int x, int y){ | |
int temp = A[x]; | |
A[x] = A[y]; | |
A[y] = temp; | |
} | |
public void quickSort(int[] A, int first, int last){ | |
if (first >= last){ | |
return; | |
} |
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
//emails: | |
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b | |
//phone numbers: | |
\(?\d{3}\)?-? *\d{3}-? *\d{4} | |
String s = "*** [email protected]&&^ [email protected]((& "; | |
Matcher m = Pattern.compile("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z0-9-.]+").matcher(s); | |
while (m.find()) { | |
System.out.println(m.group()); |
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 { |