-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk315.java
139 lines (123 loc) · 3.87 KB
/
wk315.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
package weekly;
import org.omg.CORBA.MARSHAL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class wk315 {
//ranking: 1111 / 6490 拉胯啊
//hash
public int findMaxK(int[] nums) {
int ans = -1;
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (set.contains(-num)) {
ans = Math.max(Math.abs(num), ans);
}
set.add(num);
}
return ans;
}
// hash
public int countDistinctIntegers(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
for (int num : nums) {
int nnum = 0;
while (num > 0) {
nnum = nnum * 10 + num % 10;
num /= 10;
}
set.add(nnum);
}
return set.size();
}
//暴力,其他解法不会
public boolean sumOfNumberAndReverse(int num) {
for (int i = 0; i <= num; i++) {
int n = i;
int nnum = 0;
while (n > 0) {
nnum = nnum * 10 + n % 10;
n /= 10;
}
if (i + nnum == num) return true;
}
return false;
}
/* public long check(int[] nums, int leftSub, int rightSub, int minK, int maxK) {
List<Integer> minList = new ArrayList<>();
List<Integer> maxList = new ArrayList<>();
for (int i = leftSub; i <= rightSub; i++) {
if (nums[i] == minK) {
minList.add(i);
}
if (nums[i] == maxK) {
maxList.add(i);
}
}
int[] Left = new int[nums.length];
int[] Right = new int[nums.length];
int pre = -1;
// if(nums[0]<minK||nums[0]>maxK)pre=0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < minK) {
pre = i;
}
if (nums[i] > maxK) {
pre = i;
}
Left[i] = pre;
}
pre = nums.length;
// if(nums[nums.length-1]<minK||nums[nums.length-1]>maxK)pre=nums.length-1;
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] < minK) {
pre = i;
}
if (nums[i] > maxK) {
pre = i;
}
Right[i] = pre;
}
long ans = 0;
for (int i = 0; i < minList.size(); i++) {
for (int j = 0; j < maxList.size(); j++) {
int left = Math.min(minList.get(i), maxList.get(j));
int right = Math.max(minList.get(i), maxList.get(j));
if (Right[left] < right || Left[right] > left) continue;
int l = Left[left];
int r = Right[right];
ans += (long) (left - l) * (r - right);
}
}
return ans;
}
public long countSubarrays(int[] nums, int minK, int maxK) {
long left = 0;
long ans = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < minK || nums[i] > maxK) {
}
}
return ans;
}*/
//双指针,要学会拆分子问题
public long countSubarrays(int[] nums, int minK, int maxK) {
long min = -1, max = -1, index = -1, count = 0;
//考虑以i结尾符合条件的定界子数组的个数
for (int i = 0; i < nums.length; i++) {
//更新下界的位置
min = (nums[i] == minK ? i : min);
//更新上界的位置
max = (nums[i] == maxK ? i : max);
//更新不存在上下界的位置
index = (nums[i] < minK || nums[i] > maxK ? i : index);
//以i结尾的符合条件的子数组的个数
count += Math.max(0, Math.min(min,max) - index);
}
return count;
}
}