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 { | |
ArrayList<String> wordBreak(String s, Set<String> dict) { | |
int length = s.length(); | |
//create the word ending character's index list for each character | |
ArrayList<ArrayList<Integer>> record = new ArrayList<ArrayList<Integer>>(length); | |
for(int i = 0;i<length;i++) | |
record.add(new ArrayList<Integer>()); | |
//each character can be the ending character of some word |
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 wordBreak(String s, Set<String> dict) { | |
if(s == null || s.length() == 0 || dict == null) | |
return true; | |
int length = s.length(); | |
//dp[i] is true when the substring from i to the end can be partitioned | |
boolean[] dp = new boolean[length+1]; | |
for(boolean b : dp) |
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 firstMissingPositive(int[] A) { | |
int n = A.length; | |
int i = 0; | |
while(i<n){ | |
if(A[i] > 0 && A[i] <= n && A[i]-1 != i && A[A[i]-1] != A[i]){ | |
int temp = A[A[i]-1]; | |
A[A[i]-1] = A[i]; | |
A[i] = temp; |
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 String reverseWords(String s) { | |
if(s == null) | |
return s; | |
//trim leading and trailing spaces | |
String trimmed = s.trim(); | |
//split string using space(s) as delimiter | |
String[] splitted = trimmed.split("\\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
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
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 singly-linked list with a random pointer. | |
* class RandomListNode { | |
* int label; | |
* RandomListNode next, random; | |
* RandomListNode(int x) { this.label = x; } | |
* }; | |
*/ | |
public class Solution { | |
public RandomListNode copyRandomList(RandomListNode head) { |
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 singly-linked list with a random pointer. | |
* class RandomListNode { | |
* int label; | |
* RandomListNode next, random; | |
* RandomListNode(int x) { this.label = x; } | |
* }; | |
*/ | |
public class Solution { | |
public RandomListNode copyRandomList(RandomListNode head) { |
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 singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
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
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
NewerOlder