-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkb121.java
144 lines (116 loc) · 3.5 KB
/
wkb121.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package weekly;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class wkb121 {
//遍历
public int missingInteger(int[] nums) {
int sum = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] + 1 == nums[i]) {
sum += nums[i];
} else {
break;
}
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
while (!set.contains(sum)) {
sum++;
}
return sum;
}
/* public int minOperations(int[] nums, int k) {
int sum = nums[0];
for (int i = 1; i < nums.length; i++) {
sum ^= nums[i];
}
int ans = 0;
while (k != 0 || sum != 0) {
if ((k & 1) != (sum & 1)) {
ans++;
}
k >>= 1;
sum >>= 1;
}
return ans;
}*/
//更简洁的写法
public int minOperations(int[] nums, int k) {
for (int x : nums) {
k ^= x;
}
return Integer.bitCount(k);
}
/* //贪心
public int minimumOperationsToMakeEqual(int x, int y) {
return dfs(x, y);
}
Map<Integer,Integer> memo=new HashMap<>();
int dfs(int x, int y) {
if (x <= y) {
return y - x;
}
if (memo.containsKey(x)) return memo.get(x);
int min = Math.abs(x - y);
int c1 = x % 11;
int c2 = Math.abs(11 - x % 11);
if (c1 < c2) {
min = Math.min(min, c1 + 1 + dfs((x - c1) / 11, y));
} else {
min = Math.min(min, c2 + 1 + dfs((x + c2) / 11, y));
}
int c3 = x % 5;
int c4 = Math.abs(5 - x % 5);
if (c3 < c4) {
min = Math.min(min, c3 + 1 + dfs((x - c3) / 5, y));
} else {
min = Math.min(min, c4 + 1 + dfs((x + c4) / 5, y));
}
return min;
}*/
//数位dp
public long numberOfPowerfulInt(long start, long finish, int limit, String s) {
return countDigitOne(finish, limit, s) - countDigitOne(start - 1, limit, s);
}
char s[];
long dp[];
public long countDigitOne(long n, int limit, String suffix) {
String ss = Long.toString(n);
while (ss.length() < suffix.length()) {
ss = "0" + ss;
}
s = ss.toCharArray();
dp = new long[ss.length()];
Arrays.fill(dp, -1);
return f(0, true, limit, suffix);
}
long f(int i, boolean isLimit, int limit, String suffix) {
if (i == s.length) return 1;
if (!isLimit && dp[i] >= 0) return dp[i];
long res = 0;
int up = isLimit ? s[i] - '0' : 9;
if (i < s.length - suffix.length()) {
for (int d = 0; d <= Math.min(up, limit); ++d) // 枚举要填入的数字 d
res += f(i + 1, isLimit && d == up, limit, suffix);
} else {
//受到suffix约束
int d = suffix.charAt(i - (s.length - suffix.length())) - '0';
// 判断是不是可行
if (d <= up) {
res += f(i + 1, isLimit && d == up, limit, suffix);
}
}
if (!isLimit) dp[i] = res;
return res;
}
public static void main(String[] args) {
wkb121 w = new wkb121();
long l = w.countDigitOne(6000, 4, "124");
w.numberOfPowerfulInt(1, 6000, 4, "124");
}
}