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; } | |
| * } | |
| * | |
| For example: |
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 ArrayList<ArrayList<Integer>> permute(int[] num) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| if (num.length == 0) return null; | |
| ArrayList<Integer> intList = new ArrayList<Integer>(); | |
| ArrayList<Integer> prefix = new ArrayList<Integer>(); | |
| ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
| for (int index = 0; index < num.length; index++) | |
| { |
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 atoi(String str) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| // string start with - | |
| // string is overflow the Integer.MAX_VALUE | |
| if(str.length() == 0) return 0; | |
| int right = str.length() - 1; | |
| int res = 0; |
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 { | |
| static int EVEN = 1; | |
| static int ODD = 2; | |
| public String longestPalindrome(String s) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| // Test cases: | |
| // a | |
| // aa |
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
| /* | |
| If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). | |
| The replacement must be in-place, do not allocate extra memory. | |
| Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. | |
| 1,2,3 → 1,3,2 | |
| 3,2,1 → 1,2,3 | |
| 1,1,5 → 1,5,1 | |
| */ |
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
| /* | |
| For example, given candidate set 10,1,2,7,6,1,5 and target 8, | |
| A solution set is: | |
| [1, 7] | |
| [1, 2, 5] | |
| [2, 6] | |
| [1, 1, 6] | |
| */ | |
| 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
| /* | |
| The set [1,2,3,…,n] contains a total of n! unique permutations. | |
| By listing and labeling all of the permutations in order, | |
| We get the following sequence (ie, for n = 3): | |
| "123" | |
| "132" | |
| "213" | |
| "231" |
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
| /* | |
| Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. | |
| You should preserve the original relative order of the nodes in each of the two partitions. | |
| For example, | |
| Given 1->4->3->2->5->2 and x = 3, | |
| return 1->2->2->4->3->5. | |
| */ | |
| public ListNode partition(ListNode head, int 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
| public class Solution { | |
| public String longestCommonPrefix(String[] strs) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| int size = strs.length; | |
| if(size == 0) { | |
| return ""; | |
| } | |
| String common = strs[0]; |
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 int triTiling() { | |
| if (n == 0) { | |
| return 1; | |
| } | |
| int[] f = new int[n]; | |
| int[] g = new int[n]; | |
| f[0] = 1; | |
| f[1] = 0; | |
| g[1] = 1; | |
| g[0] = 0; |