Skip to content

Commit 66024c3

Browse files
committed
house_robber && number_of_1bits
1 parent ad90163 commit 66024c3

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
155155
#### [187. Repeated DNA Sequences](https://github.com/hitzzc/go-leetcode/tree/master/repeated_dna_sequences)
156156
#### [189. Rotate Array](https://github.com/hitzzc/go-leetcode/tree/master/rotate_array)
157157
#### [190. Reverse Bits](https://github.com/hitzzc/go-leetcode/tree/master/reverse_bits)
158+
#### [191. Number of 1 Bits](https://github.com/hitzzc/go-leetcode/tree/master/number_of_1bits)
159+
#### [198. House Robber](https://github.com/hitzzc/go-leetcode/tree/master/house_robber)
160+
158161

159162

160163

house_robber/house_robber.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package house_robber
2+
3+
func rob(nums []int) int {
4+
if len(nums) == 0 {
5+
return 0
6+
}
7+
if len(nums) == 1 {
8+
return nums[0]
9+
}
10+
array := make([]int, len(nums))
11+
array[0] = nums[0]
12+
if array[0] > nums[1] {
13+
array[1] = array[0]
14+
} else {
15+
array[1] = nums[1]
16+
}
17+
for i := 2; i < len(nums); i++ {
18+
if array[i-1] > array[i-2]+nums[i] {
19+
array[i] = array[i-1]
20+
} else {
21+
array[i] = array[i-2] + nums[i]
22+
}
23+
}
24+
return array[len(array)-1]
25+
}

number_of_1bits/number_of_1bits.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int hammingWeight(uint32_t n) {
4+
int x = 1;
5+
int count = 0;
6+
for (int i = 0; i < 32; ++i){
7+
if (n & x) count++;
8+
x <<= 1;
9+
}
10+
return count;
11+
}
12+
};

0 commit comments

Comments
 (0)