Skip to content

Commit b8df499

Browse files
authored
Merge pull request #1295 from bugyun/master
641-Week 07 08
2 parents fd6cd03 + 6b8aecf commit b8df499

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package vip.ruoyun.week7.lesson16;
2+
3+
public class LeetCode_191_641 {
4+
5+
6+
//利用 and 操作符
7+
public int hammingWeight(int n) {
8+
int bits = 0;
9+
int mask = 1;
10+
for (int i = 0; i < 32; i++) {
11+
if ((n & mask) != 0) {
12+
bits++;
13+
}
14+
mask <<= 1;
15+
}
16+
return bits;
17+
}
18+
19+
//每回都看看是否等于 0 如果不等于 0 那么就 and n-1 位
20+
public int hammingWeight2(int n) {
21+
int sum = 0;
22+
while (n != 0) {
23+
sum++;
24+
n &= (n - 1);
25+
}
26+
return sum;
27+
}
28+
29+
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package vip.ruoyun.week7.lesson16;
2+
3+
public class LeetCode_231_641 {
4+
5+
//2的幂数的数字的二进制有且只有一个1,其余均是0
6+
public boolean isPowerOfTwo(int n) {
7+
return n > 0 && (n & (n - 1)) == 0;
8+
}
9+
}
10+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package vip.ruoyun.week7.lesson18;
2+
3+
public class LeetCode_1122_641 {
4+
5+
6+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
7+
int[] nums = new int[1001];
8+
int[] res = new int[arr1.length];
9+
//遍历arr1,统计每个元素的数量
10+
for (int i : arr1) {
11+
nums[i]++;
12+
}
13+
//遍历arr2,处理arr2中出现的元素
14+
int index = 0;
15+
for (int i : arr2) {
16+
while (nums[i] > 0) {
17+
res[index++] = i;
18+
nums[i]--;
19+
}
20+
}
21+
//遍历nums,处理剩下arr2中未出现的元素
22+
for (int i = 0; i < nums.length; i++) {
23+
while (nums[i] > 0) {
24+
res[index++] = i;
25+
nums[i]--;
26+
}
27+
}
28+
return res;
29+
}
30+
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package vip.ruoyun.week8.lesson19;
2+
3+
public class LeetCode_300_641 {
4+
5+
//动态规划
6+
public int lengthOfLIS(int[] nums) {
7+
int[] tails = new int[nums.length];
8+
int res = 0;
9+
for (int num : nums) {
10+
int i = 0, j = res;
11+
while (i < j) {
12+
int m = (i + j) / 2;
13+
if (tails[m] < num) i = m + 1;
14+
else j = m;
15+
}
16+
tails[i] = num;
17+
if (res == j) res++;
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package vip.ruoyun.week8.lesson19;
2+
3+
public class LeetCode_91_641 {
4+
5+
public int numDecodings(String s) {
6+
return getAns(s, 0);
7+
}
8+
9+
private int getAns(String s, int start) {
10+
//划分到了最后返回 1
11+
if (start == s.length()) {
12+
return 1;
13+
}
14+
//开头是 0,0 不对应任何字母,直接返回 0
15+
if (s.charAt(start) == '0') {
16+
return 0;
17+
}
18+
//得到第一种的划分的解码方式
19+
int ans1 = getAns(s, start + 1);
20+
int ans2 = 0;
21+
//判断前两个数字是不是小于等于 26 的
22+
if (start < s.length() - 1) {
23+
int ten = (s.charAt(start) - '0') * 10;
24+
int one = s.charAt(start + 1) - '0';
25+
if (ten + one <= 26) {
26+
ans2 = getAns(s, start + 2);
27+
}
28+
}
29+
return ans1 + ans2;
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package vip.ruoyun.week8.lesson20;
2+
3+
import java.util.HashMap;
4+
5+
public class LeetCode_387_641 {
6+
public int firstUniqChar(String s) {
7+
HashMap<Character, Integer> count = new HashMap<Character, Integer>();
8+
int n = s.length();
9+
// build hash map : character and how often it appears
10+
for (int i = 0; i < n; i++) {
11+
char c = s.charAt(i);
12+
count.put(c, count.getOrDefault(c, 0) + 1);
13+
}
14+
15+
// find the index
16+
for (int i = 0; i < n; i++) {
17+
if (count.get(s.charAt(i)) == 1)
18+
return i;
19+
}
20+
return -1;
21+
}
22+
}

0 commit comments

Comments
 (0)