Skip to content

Commit 1b0d9b2

Browse files
committed
commit
Change-Id: Iff97484d4b18b71009ad166d6370bade0adbab35
1 parent 931f4b3 commit 1b0d9b2

File tree

14 files changed

+456
-72
lines changed

14 files changed

+456
-72
lines changed

.idea/modules.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 85 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.Arrays;
2+
import java.util.Map;
3+
import java.util.SortedMap;
4+
import java.util.TreeMap;
5+
6+
/**
7+
* Author: 王俊超
8+
* Time: 2020-07-04 15:09
9+
* CSDN: http://blog.csdn.net/derrantcm
10+
* Github: https://github.com/Wang-Jun-Chao
11+
* Declaration: All Rights Reserved !!!
12+
**/
13+
public class Solution {
14+
15+
/**
16+
* 求每个元素的个数,再找相邻元素之和最大的
17+
* 相邻指元素之差为1
18+
*
19+
* @param nums
20+
* @return
21+
*/
22+
public int findLHS(int[] nums) {
23+
if (nums == null || nums.length < 2) {
24+
return 0;
25+
}
26+
27+
int max = 0;
28+
Arrays.sort(nums);
29+
30+
int prevNum = Integer.MIN_VALUE;
31+
int prevCnt = 0;
32+
for (int i = 0; i < nums.length; ) {
33+
int next = i + 1;
34+
while (next < nums.length && nums[next] == nums[i]) {
35+
next++;
36+
}
37+
if (prevNum + 1 == nums[i]) {
38+
max = Math.max(max, prevCnt + next - i);
39+
}
40+
41+
prevNum = nums[i];
42+
prevCnt = next - i;
43+
i = next;
44+
45+
}
46+
47+
return max;
48+
}
49+
50+
/**
51+
* 求每个元素的个数,再找相邻元素之和最大的
52+
* 相邻指元素之差为1
53+
*
54+
* @param nums
55+
* @return
56+
*/
57+
public int findLHS2(int[] nums) {
58+
if (nums == null || nums.length < 2) {
59+
return 0;
60+
}
61+
62+
SortedMap<Integer, Integer> map = new TreeMap<>();
63+
for (int n : nums) {
64+
map.put(n, map.getOrDefault(n, 0) + 1);
65+
}
66+
67+
int max = 0;
68+
Map.Entry<Integer, Integer> prev = null; // 前一个遍历的元素
69+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
70+
if (prev != null && prev.getKey() + 1 == entry.getKey()) { // 相邻元素
71+
max = Math.max(max, prev.getValue() + entry.getValue()); // 取较大的
72+
}
73+
prev = entry;
74+
}
75+
76+
return max;
77+
}
78+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import org.junit.Assert;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 15:20
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class SolutionTest {
11+
12+
@org.junit.Test
13+
public void findLHS() {
14+
Solution s = new Solution();
15+
Object[][] data = {
16+
{new int[]{1, 3, 2, 2, 5, 2, 3, 7}, 5},
17+
{new int[]{-2, 0, 2, 2, 5, 5, 5, 5}, 0},
18+
{new int[]{1, 1, 1, 1}, 0},
19+
};
20+
21+
for (Object[] d : data) {
22+
int result = s.findLHS((int[]) d[0]);
23+
Assert.assertEquals(d[1], result);
24+
}
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Author: 王俊超
3+
* Time: 2020-07-04 15:50
4+
* CSDN: http://blog.csdn.net/derrantcm
5+
* Github: https://github.com/Wang-Jun-Chao
6+
* Declaration: All Rights Reserved !!!
7+
**/
8+
public class Solution {
9+
/**
10+
* 数组的第一元素一定是最大的,要找最大的元素个数,只要找ops中min(ops[0])*min(ops[1])
11+
*
12+
* @param m
13+
* @param n
14+
* @param ops
15+
* @return
16+
*/
17+
public int maxCount(int m, int n, int[][] ops) {
18+
19+
20+
int row = m;
21+
int col = n;
22+
for (int[] op : ops) {
23+
row = Math.min(op[0], row);
24+
col = Math.min(op[1], col);
25+
}
26+
27+
return row * col;
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import org.junit.Assert;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 16:04
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class SolutionTest {
11+
12+
@org.junit.Test
13+
public void maxCount() {
14+
Solution s = new Solution();
15+
Object[][] data = {
16+
{3, 3, new int[][]{{2, 2}, {3, 3}}, 4},
17+
{3, 3, new int[][]{{2, 3}, {3, 2}}, 4},
18+
{3, 3, new int[][]{}, 9},
19+
};
20+
21+
for (Object[] d : data) {
22+
Object result = s.maxCount((int) d[0], (int)d[1], (int[][]) d[2]);
23+
Assert.assertEquals(d[3], result);
24+
}
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
/**
4+
* Author: 王俊超
5+
* Time: 2020-07-04 16:13
6+
* CSDN: http://blog.csdn.net/derrantcm
7+
* Github: https://github.com/Wang-Jun-Chao
8+
* Declaration: All Rights Reserved !!!
9+
**/
10+
public class Solution {
11+
public String[] findRestaurant(String[] list1, String[] list2) {
12+
if (list1 == null || list2 == null){
13+
return null;
14+
}
15+
16+
Map<String,Integer> map = new HashMap<>();
17+
for (int i = 0; i < list1.length; i++) {
18+
map.put(list1[i], i);
19+
}
20+
List<String> result = new ArrayList<>(list1.length);
21+
22+
int min = Integer.MAX_VALUE;
23+
24+
for (int i = 0; i < list2.length; i++) {
25+
if (map.containsKey(list2[i])) {
26+
if (map.get(list2[i]) + i < min) {
27+
result.clear();
28+
min = map.get(list2[i]) + i;
29+
result.add(list2[i]);
30+
} else if (map.get(list2[i]) + i == min) {
31+
result.add(list2[i]);
32+
}
33+
34+
}
35+
}
36+
37+
return result.toArray(new String[0]);
38+
}
39+
}

0 commit comments

Comments
 (0)