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 reverseWords(String s) { | |
s = s.trim(); | |
int n = s.length(); | |
String str = ""; | |
while(s.lastIndexOf(" ") != -1){ | |
int index = s.lastIndexOf(" "); | |
str = str + s.substring(index + 1, n) + " "; | |
s = s.substring(0, index).trim(); | |
n = s.length(); |
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 singleNumber(int[] A) { | |
int[] count = new int[32]; | |
int result = 0; | |
for (int i = 0; i < 32; i++) { | |
for (int j = 0; j < A.length; j++) { | |
if (((A[j] >> i) & 1) == 1) { | |
count[i]++; | |
} | |
} |
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 singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { | |
* val = x; | |
* next = null; | |
* } | |
* } |
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 maxProfit(int[] prices) { | |
int ret = 0; | |
int index = 0; | |
while (index < prices.length){ | |
if (index + 1 < prices.length){ | |
if (prices[index] < prices[index + 1]){ | |
ret += prices[index + 1] - prices[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 numTrees(int n) { | |
if (n == 0) return 1; | |
if (n == 1) return 1; | |
if (n == 2) return 2; | |
int ret = 0; | |
for (int i = 1; i <= n; i++){ | |
ret = ret + numTrees(i - 1) * numTrees(n - i); | |
} | |
return ret; |
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; } | |
* } | |
*/ | |
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 climbStairs(int n) { | |
if (n == 0) return 0; | |
if (n == 1) return 1; | |
if (n == 2) return 2; | |
int ret = 0; | |
int tempa = 2; | |
int tempb = 1; | |
for (int i = 3; i <= n; i++){ | |
ret = tempa + tempb; |
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 sum = Integer.MIN_VALUE; | |
int ret = Integer.MIN_VALUE; | |
int length = A.length; | |
for(int i = 0; i < A.length; i++){ | |
if (sum < 0){ | |
sum = A[i]; | |
}else{ | |
sum += A[i]; |