Skip to content

Commit 3378e93

Browse files
committed
20160426
1 parent aa3b000 commit 3378e93

File tree

13 files changed

+197
-23
lines changed

13 files changed

+197
-23
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int rob(int[] nums) {
3+
if (nums.length==0) {
4+
return 0;
5+
}
6+
if (nums.length==1) {
7+
return nums[0];
8+
}
9+
int[] dp=new int[nums.length];
10+
//第一家到倒数第二家
11+
dp[0] = nums[0];
12+
for (int i = 1; i < nums.length-1; i++) {
13+
dp[i]=Math.max(dp[i-1], (i==1?0:dp[i-2])+nums[i]);
14+
}
15+
int res=dp[nums.length-2];
16+
//第二家到倒数第一家
17+
dp[1] = nums[1];
18+
for (int i = 2; i < nums.length; i++) {
19+
dp[i]=Math.max(dp[i-1], (i==2?0:dp[i-2])+nums[i]);
20+
}
21+
return Math.max(res, dp[dp.length-1]);
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public int rob(TreeNode root) {
12+
return root==null?0:Math.max(root.val+robSon(root.left)+robSon(root.right), rob(root.left)+rob(root.right));
13+
}
14+
public int robSon(TreeNode root){
15+
return root==null?0:rob(root.left)+rob(root.right);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
List<Integer> list=new ArrayList<Integer>(){
3+
{
4+
add(0);
5+
}
6+
};
7+
public int[] countBits(int num) {
8+
if (num>=list.size()) {
9+
for (int i = list.size(); i < num+1; i++) {
10+
list.add(1+list.get(i-(int)Math.pow(2, (int)(Math.log(i)/Math.log(2)))));
11+
}
12+
}
13+
int[] ans=new int[num+1];
14+
for (int i = 0; i < ans.length; i++) {
15+
ans[i]=list.get(i);
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public String reverseString(String s) {
3+
return new StringBuffer(s).reverse().toString();
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public String reverseVowels(String s) {
3+
char[] cs=s.toCharArray();
4+
int i=0;
5+
int j=cs.length-1;
6+
while (i<j) {
7+
while (i<j&&cs[i]!=65&&cs[i]!=69&&cs[i]!=73&&cs[i]!=79&&cs[i]!=85&&cs[i]!=97&&cs[i]!=101&&cs[i]!=105&&cs[i]!=111&&cs[i]!=117) {
8+
i++;
9+
}
10+
while (i<j&&cs[j]!=65&&cs[j]!=69&&cs[j]!=73&&cs[j]!=79&&cs[j]!=85&&cs[j]!=97&&cs[j]!=101&&cs[j]!=105&&cs[j]!=111&&cs[j]!=117) {
11+
j--;
12+
}
13+
if (i<j) {
14+
char c=cs[i];
15+
cs[i]=cs[j];
16+
cs[j]=c;
17+
i++;
18+
j--;
19+
}
20+
}
21+
return String.valueOf(cs);
22+
}
23+
}

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
|210|[Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)|Medium|noNote|no|no|no|no|
197197
|211|[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)|Medium|noNote|no|no|no|no|
198198
|212|[Word Search II](https://leetcode.com/problems/word-search-ii/)|Hard|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/212. Word Search II/Solution.java)|no|no|no|
199-
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)|Medium|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/213. House Robber II/Solution.py)|no|no|
199+
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/213. House Robber II/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/213. House Robber II/Solution.py)|no|no|
200200
|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)|Hard|noNote|no|no|no|no|
201201
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/215. Kth Largest Element in an Array/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/215. Kth Largest Element in an Array/Solution.py)|no|no|
202202
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/216. Combination Sum III/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/216. Combination Sum III/Solution.py)|no|no|
@@ -319,12 +319,12 @@
319319
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|Medium|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/334. Increasing Triplet Subsequence/Solution.py)|no|no|
320320
|335|[Self Crossing](https://leetcode.com/problems/self-crossing/)|Hard|noNote|no|no|no|no|
321321
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/)|Hard|noNote|no|no|no|no|
322-
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|Medium|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/337. House Robber III/Solution.py)|no|no|
323-
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|Medium|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/338. Counting Bits/Solution.py)|no|no|
322+
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/337. House Robber III/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/337. House Robber III/Solution.py)|no|no|
323+
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/338. Counting Bits/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/338. Counting Bits/Solution.py)|no|no|
324324
|339|[Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/)|Easy|noBuy|noBuy|noBuy|noBuy|noBuy|
325325
|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)|Hard|noBuy|noBuy|noBuy|noBuy|noBuy|
326326
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/341. Flatten Nested List Iterator/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/341. Flatten Nested List Iterator/Solution.py)|no|no|
327327
|342|[Power of Four](https://leetcode.com/problems/power-of-four/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/342. Power of Four/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/342. Power of Four/Solution.py)|no|no|
328328
|343|[Integer Break](https://leetcode.com/problems/integer-break/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/343. Integer Break/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/343. Integer Break/Solution.py)|no|no|
329-
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|Easy|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/344. Reverse String/Solution.py)|no|no|
330-
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|Easy|noNote|no|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/345. Reverse Vowels of a String/Solution.py)|no|no|
329+
|344|[Reverse String](https://leetcode.com/problems/reverse-string/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/344. Reverse String/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/344. Reverse String/Solution.py)|no|no|
330+
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/345. Reverse Vowels of a String/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/345. Reverse Vowels of a String/Solution.py)|no|no|

java/001. Two Sum.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
public class Solution {
2-
public int[] twoSum(int[] numbers, int target) {
3-
int[] res = new int[2];
4-
Map map = new HashMap();
5-
for (int i = 0; i < numbers.length; i++) {
6-
map.put(numbers[i], i);
7-
}
8-
for (int i = 0; i < numbers.length; i++) {
9-
int gap = target - numbers[i];
10-
if (map.get(gap) != null && (int) map.get(gap) != i) {
11-
res[0] = i + 1;
12-
res[1] = (int) map.get(gap) + 1;
13-
break;
14-
}
15-
}
16-
return res;
17-
}
1+
public class Solution {
2+
public int[] twoSum(int[] numbers, int target) {
3+
int[] res = new int[2];
4+
Map map = new HashMap();
5+
for (int i = 0; i < numbers.length; i++) {
6+
map.put(numbers[i], i);
7+
}
8+
for (int i = 0; i < numbers.length; i++) {
9+
int gap = target - numbers[i];
10+
if (map.get(gap) != null && (int) map.get(gap) != i) {
11+
res[0] = i + 1;
12+
res[1] = (int) map.get(gap) + 1;
13+
break;
14+
}
15+
}
16+
return res;
17+
}
1818
}

java/002. Add Two Numbers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Definition for singly-linked list.
33
* public class ListNode {
44
* int val;

java/213. House Robber II.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int rob(int[] nums) {
3+
if (nums.length==0) {
4+
return 0;
5+
}
6+
if (nums.length==1) {
7+
return nums[0];
8+
}
9+
int[] dp=new int[nums.length];
10+
//第一家到倒数第二家
11+
dp[0] = nums[0];
12+
for (int i = 1; i < nums.length-1; i++) {
13+
dp[i]=Math.max(dp[i-1], (i==1?0:dp[i-2])+nums[i]);
14+
}
15+
int res=dp[nums.length-2];
16+
//第二家到倒数第一家
17+
dp[1] = nums[1];
18+
for (int i = 2; i < nums.length; i++) {
19+
dp[i]=Math.max(dp[i-1], (i==2?0:dp[i-2])+nums[i]);
20+
}
21+
return Math.max(res, dp[dp.length-1]);
22+
}
23+
}

java/337. House Robber III.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public int rob(TreeNode root) {
12+
return root==null?0:Math.max(root.val+robSon(root.left)+robSon(root.right), rob(root.left)+rob(root.right));
13+
}
14+
public int robSon(TreeNode root){
15+
return root==null?0:rob(root.left)+rob(root.right);
16+
}
17+
}

java/338. Counting Bits.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
List<Integer> list=new ArrayList<Integer>(){
3+
{
4+
add(0);
5+
}
6+
};
7+
public int[] countBits(int num) {
8+
if (num>=list.size()) {
9+
for (int i = list.size(); i < num+1; i++) {
10+
list.add(1+list.get(i-(int)Math.pow(2, (int)(Math.log(i)/Math.log(2)))));
11+
}
12+
}
13+
int[] ans=new int[num+1];
14+
for (int i = 0; i < ans.length; i++) {
15+
ans[i]=list.get(i);
16+
}
17+
return ans;
18+
}
19+
}

java/344. Reverse String.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public String reverseString(String s) {
3+
return new StringBuffer(s).reverse().toString();
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public String reverseVowels(String s) {
3+
char[] cs=s.toCharArray();
4+
int i=0;
5+
int j=cs.length-1;
6+
while (i<j) {
7+
while (i<j&&cs[i]!=65&&cs[i]!=69&&cs[i]!=73&&cs[i]!=79&&cs[i]!=85&&cs[i]!=97&&cs[i]!=101&&cs[i]!=105&&cs[i]!=111&&cs[i]!=117) {
8+
i++;
9+
}
10+
while (i<j&&cs[j]!=65&&cs[j]!=69&&cs[j]!=73&&cs[j]!=79&&cs[j]!=85&&cs[j]!=97&&cs[j]!=101&&cs[j]!=105&&cs[j]!=111&&cs[j]!=117) {
11+
j--;
12+
}
13+
if (i<j) {
14+
char c=cs[i];
15+
cs[i]=cs[j];
16+
cs[j]=c;
17+
i++;
18+
j--;
19+
}
20+
}
21+
return String.valueOf(cs);
22+
}
23+
}

0 commit comments

Comments
 (0)