Skip to content

Commit ca8cc5c

Browse files
author
caikun1
committed
左右偏向问题
1 parent 76e9d1e commit ca8cc5c

File tree

4 files changed

+181
-21
lines changed

4 files changed

+181
-21
lines changed

src/main/java/ind/ck/lambda/LambdaTest.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ public void shelf() {
8888

8989
public static void predicateTest() {
9090
Predicate<UserObj> predicate = userObj -> userObj.age <= 18 && Objects.equals(userObj.name, "kangxi");
91-
9291
System.out.println(predicate.test(user1));
9392
System.out.println(predicate.test(user2));
9493
}
9594

9695
public static void SupplierTest() {
97-
Supplier<UserObj> supplier = () -> new UserObj(UUID.randomUUID().toString(), Integer.valueOf((Math.random() * 10) + ""));
96+
Supplier<UserObj> supplier = () -> new UserObj("qianlong", 60);
97+
Consumer<UserObj> consumer = System.out::println;
9898

9999
}
100100

@@ -128,19 +128,17 @@ public static void streamTest() {
128128
new UserObj("b", 25),
129129
new UserObj("c", 17),
130130
new UserObj("d", 13)).collect(Collectors.toList());
131-
List<UserObj> heis = Stream.of(
131+
List<UserObj> heis1 = Stream.of(
132132
new UserObj("a", 13),
133133
new UserObj("m1", 30)).collect(Collectors.toList());
134134
List<UserObj> heis2 = Stream.of(
135135
new UserObj("a", 93),
136136
new UserObj("b", 55)).collect(Collectors.toList());
137137
//
138-
Stream<UserObj> userObjStream = uList.stream().filter(userObj -> userObj.age < 24);
139-
Stream<Map<String, Object>> mapStream = userObjStream.map(UserObj::toMap);
138+
Stream<Map<String, Object>> mapStream = uList.stream().filter(userObj -> userObj.age < 24).map(UserObj::toMap);
140139

141140
// flatmap这样就给两个流合并了
142-
Stream<List<UserObj>> uList1 = Stream.of(uList, heis, heis2);
143-
Stream<UserObj> userObjStream1 = uList1.flatMap(Collection::stream);
141+
Stream<UserObj> userObjStream1 = Stream.of(uList, heis1, heis2).flatMap(Collection::stream);
144142
// 求个最大值
145143
// UserObj userObj = userObjStream1.max(Comparator.comparing(st -> st.age)).get();
146144
// System.out.println("count:" + userObj);
@@ -150,6 +148,7 @@ public static void streamTest() {
150148
// return x;
151149
// });
152150
// System.out.println(reduce.get());
151+
// System.out.println(reduce.get());
153152
// 求平均年龄
154153
// userObjStream1.collect()
155154

@@ -162,12 +161,15 @@ public static void streamTest() {
162161
// Map<String, List<UserObj>> collect = userObjStream1.collect(Collectors.groupingBy(UserObj::getName));
163162
// System.out.println(collect);
164163

165-
// grouping 分组聚合
164+
// grouping 分组聚合1
166165
// Map<String, Integer> collect = userObjStream1.collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)));
167166
// System.out.println(collect);
167+
// grouping 分组聚合2
168+
Integer collect = userObjStream1.collect(Collectors.collectingAndThen(Collectors.groupingBy(UserObj::getName), Map::size));
169+
System.out.println(collect);
168170
// 拼接
169171
// Collectors.collectingAndThen(Collectors.toList(), Optional::ofNullable)
170-
String collect = userObjStream1.map(UserObj::getName).collect(Collectors.joining(",", " ", " "));
172+
// String collect = userObjStream1.map(UserObj::getName).collect(Collectors.joining(",", " ", " "));
171173
System.out.println(collect);
172174
}
173175

@@ -185,16 +187,16 @@ public static void streamTestAdv() {
185187
// return u;
186188
// });
187189
// 轻松转map
188-
// Map<String, UserObj> collect = uList.stream()
189-
//// .collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)))
190-
// .collect(Collectors.toMap(UserObj::getName, Function.identity()));
191-
// System.out.println(collect);
190+
Map<String, UserObj> collect = uList.stream()
191+
// .collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)))
192+
.collect(Collectors.toMap(UserObj::getName, Function.identity()));
193+
System.out.println(collect);
192194

193195

194-
Map<Boolean, List<UserObj>> collect = uList.stream().collect(
195-
Collectors.partitioningBy(
196-
u -> u.getAge() > 18)
197-
);
196+
// Map<Boolean, List<UserObj>> collect = uList.stream().collect(
197+
// Collectors.partitioningBy(
198+
// u -> u.getAge() > 18)
199+
// );
198200
System.out.println(collect);
199201
}
200202

@@ -236,7 +238,7 @@ public static void streamTestPar2() {
236238
String[] intFrs = new String[] {"1","2","3","4","5","6","7","8","9","10"};
237239
// new
238240
Stream.of(intFrs)
239-
.parallel()
241+
// .parallel()
240242
.mapToLong(Long::parseLong)
241243
.mapToObj(fr -> {
242244
AsOs key = new AsOs();
@@ -277,10 +279,10 @@ public static void acc() {
277279
public static void main(String[] args) {
278280
// predicateTest();
279281
// consumerTest();
280-
// streamTest();
282+
streamTest();
281283
// streamTestAdv();
282284
// streamTestHM();
283-
streamTestPar2();
285+
// streamTestPar2();
284286
}
285287

286288
}

src/main/java/ind/ck/leetcode/ArrayTest.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,55 @@ public static int lengthOfLongestSubstring(String s) {
105105
}
106106

107107

108+
public static int leftIdx(int[] nums, int target) {
109+
int l = 0;
110+
int r = nums.length - 1;
111+
while (l < r) {
112+
int mid = (r - l) / 2 + l;
113+
if (nums[mid] >= target) {
114+
r = mid;
115+
116+
} else {
117+
l = mid + 1;
118+
}
119+
}
120+
return r;
121+
}
122+
// 0,1,2,3
123+
// 3,2,1,0
124+
// 1,1,1,1,2,2 -> 1
125+
public static int rightIdx(int[] nums, int target) {
126+
int l = 0;
127+
int r = nums.length - 1;
128+
while (l < r) {
129+
int mid = (r - l + 1) / 2 + l;
130+
if (nums[mid] <= target) {
131+
l = mid;
132+
} else {
133+
r = mid - 1;
134+
}
135+
}
136+
return r;
137+
}
138+
139+
108140
public static void main(String[] args) {
109-
System.out.println(lengthOfLongestSubstring("abcabcbb"));
141+
// System.out.println(lengthOfLongestSubstring("abcabcbb"));
142+
int[] nums = new int[] {1,1,2,2,2,2,3,3,4};
143+
System.out.println(leftIdx(nums, 1));
144+
System.out.println(leftIdx(nums, 2));
145+
System.out.println(leftIdx(nums, 3));
146+
System.out.println(leftIdx(nums, 4));
147+
System.out.println(leftIdx(nums, 0));
148+
149+
System.out.println("==============");
150+
System.out.println(rightIdx(nums, 1));
151+
System.out.println(rightIdx(nums, 2));
152+
System.out.println(rightIdx(nums, 3));
153+
System.out.println(rightIdx(nums, 4));
154+
System.out.println(rightIdx(nums, 0));
155+
156+
110157
// System.out.println(longestCommonPrefix(new String[]{"cog","dacecar","dar"}));
111158

112159
// String ab = "abc";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ind.ck.playground;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* @Author caikun
7+
* @Description
8+
* @Date 上午10:48 21-7-15
9+
**/
10+
public class ArrayListLab {
11+
12+
public static void main(String[] args) {
13+
ArrayList<Integer> integerArrayList = new ArrayList<>(1);
14+
integerArrayList.ensureCapacity(Integer.MAX_VALUE);
15+
16+
17+
18+
19+
20+
}
21+
22+
23+
}
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package ind.ck.sort;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
/**
7+
* @Author caikun
8+
* @Description
9+
*
10+
*
11+
* @Date 下午4:47 21-7-6
12+
**/
13+
public class GreatSort {
14+
15+
private static int[] tp = new int[] {3, 5, 2, 1, 15, 22, 4, 99};
16+
17+
18+
private static int[] aa1 = new int[] {9, 12, 2, 7};
19+
// todo:警告!不能直接mergr
20+
// 2, 12* , _9*, 7
21+
//
22+
private static int[] aa2 = new int[] {2,4,5,6,7,9};
23+
// 归并排序
24+
public static void mergeSort(int[] ts, int l, int r) {
25+
if (l < r) {
26+
int mid = (l + r) / 2;
27+
mergeSort(ts, l, mid);
28+
mergeSort(ts, mid + 1, r);
29+
merge(l, mid, r, ts);
30+
}
31+
}
32+
// 0 1 2 3 ;mid= 1 , l = 0, r = 3
33+
// 0 1 2 3 4 ; mid = 2
34+
private static void merge(int l, int mid, int r, int[] ints) {
35+
//
36+
int[] am = Arrays.copyOfRange(ints, 0, r + 1);
37+
int i = l;
38+
int ri = mid + 1;
39+
while (l <= mid && ri <= r ) {
40+
if (am[l] > am[ri]) {
41+
// 左大
42+
// 交换;左++
43+
ints[i] = am[ri];
44+
ri ++;
45+
} else {
46+
// 左小
47+
// 左++
48+
ints[i] = am[l];
49+
l ++;
50+
}
51+
i ++;
52+
53+
}
54+
// complements
55+
while (l <= mid) {
56+
ints[i ++] = am[l ++];
57+
}
58+
while (ri <= r) {
59+
ints[i ++] = am[ri ++];
60+
}
61+
}
62+
63+
public static void main(String[] args) {
64+
int c = 1000000007;
65+
System.out.println("Integer.MAX_VALUE :" + Integer.MAX_VALUE);
66+
int a = Integer.MAX_VALUE * 2;
67+
int b = (Integer.MAX_VALUE % c) * (2 % c);
68+
long la = Integer.MAX_VALUE * 2L;
69+
long lb = (Integer.MAX_VALUE % c) * (2 % c);
70+
System.out.println("a :" + a);
71+
System.out.println("b :" + b);
72+
System.out.println("la :" + la);
73+
System.out.println("lb :" + lb);
74+
75+
System.out.println((-3 % c));
76+
}
77+
78+
79+
// public static void main(String[] args) {
80+
//// merge(0, 1, aa1.length - 1, aa1);
81+
//// System.out.println(Arrays.toString(aa1));
82+
// mergeSort(tp, 0, tp.length - 1);
83+
// System.out.println(Arrays.toString(tp));
84+
// }
85+
86+
87+
88+
}

0 commit comments

Comments
 (0)