Skip to content

Commit 5ffe352

Browse files
committed
fd
1 parent 15c67d0 commit 5ffe352

File tree

2 files changed

+39
-101
lines changed

2 files changed

+39
-101
lines changed

solution/src/main/java/com/inuker/solution/SearchForARange.java

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,21 @@
77
public class SearchForARange {
88

99
public int[] searchRange(int[] nums, int target) {
10-
int len = nums.length;
11-
12-
int left, right;
13-
14-
int[] result = new int[] { -1, -1 };
15-
16-
for (left = 0, right = len - 1; left < right; ) {
17-
int mid = (left + right) / 2;
18-
19-
if (target > nums[mid]) {
20-
left = mid + 1;
21-
} else {
22-
right = mid;
23-
}
10+
if (nums.length == 0) {
11+
return new int[]{-1, -1};
2412
}
25-
26-
if (nums[left] != target) {
27-
return result;
28-
} else {
29-
result[0] = left;
30-
}
31-
32-
for (right = len - 1; left < right; ) {
33-
int mid = (left + right) / 2 + 1;
34-
35-
if (target < nums[mid]) {
36-
right = mid - 1;
37-
} else {
38-
left = mid;
39-
}
40-
}
41-
42-
result[1] = right;
43-
44-
return result;
13+
return new int[] {
14+
lowerBound(nums, target),
15+
upperBound(nums, target)
16+
};
4517
}
4618

4719
/**
48-
// 测试用例
49-
// [5,7], [3,5], [5,5], [3,4], [6,7], [3,6], target = 5
20+
* lowerBound和upperBound的TestCase
21+
* [1,3],3
22+
* [1,3],1
23+
* [2,2],2
24+
*/
5025
public static int lowerBound(int[] nums, int target) {
5126
int left = 0, right = nums.length - 1;
5227

@@ -78,5 +53,4 @@ public static int upperBound(int[] nums, int target) {
7853

7954
return nums[right] == target ? right : -1;
8055
}
81-
*/
8256
}

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 28 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,81 +20,45 @@
2020

2121
public class Test2 {
2222

23-
public class AutocompleteSystem {
24-
25-
class TrieNode {
26-
Map<Character, TrieNode> children;
27-
Map<String, Integer> counts;
28-
boolean isWord;
29-
public TrieNode() {
30-
children = new HashMap<Character, TrieNode>();
31-
counts = new HashMap<String, Integer>();
32-
isWord = false;
33-
}
34-
}
35-
36-
class Pair {
37-
String s;
38-
int c;
39-
public Pair(String s, int c) {
40-
this.s = s; this.c = c;
41-
}
23+
public int[] searchRange(int[] nums, int target) {
24+
if (nums.length == 0) {
25+
return new int[]{-1, -1};
4226
}
27+
return new int[] {
28+
lowerBound(nums, target),
29+
upperBound(nums, target)
30+
};
31+
}
4332

44-
TrieNode root;
45-
String prefix;
46-
33+
public static int lowerBound(int[] nums, int target) {
34+
int left = 0, right = nums.length - 1;
4735

48-
public AutocompleteSystem(String[] sentences, int[] times) {
49-
root = new TrieNode();
50-
prefix = "";
36+
while (left < right) {
37+
int mid = left + ((right - left) >> 1);
5138

52-
for (int i = 0; i < sentences.length; i++) {
53-
add(sentences[i], times[i]);
39+
if (target > nums[mid]) {
40+
left = mid + 1;
41+
} else {
42+
right = mid;
5443
}
5544
}
5645

57-
private void add(String s, int count) {
58-
TrieNode curr = root;
59-
for (char c : s.toCharArray()) {
60-
TrieNode next = curr.children.get(c);
61-
if (next == null) {
62-
next = new TrieNode();
63-
curr.children.put(c, next);
64-
}
65-
curr = next;
66-
curr.counts.put(s, curr.counts.getOrDefault(s, 0) + count);
67-
}
68-
curr.isWord = true;
69-
}
46+
return nums[left] == target ? left : -1;
47+
}
7048

71-
public List<String> input(char c) {
72-
if (c == '#') {
73-
add(prefix, 1);
74-
prefix = "";
75-
return new ArrayList<String>();
76-
}
49+
public static int upperBound(int[] nums, int target) {
50+
int left = 0, right = nums.length - 1;
7751

78-
prefix = prefix + c;
79-
TrieNode curr = root;
80-
for (char cc : prefix.toCharArray()) {
81-
TrieNode next = curr.children.get(cc);
82-
if (next == null) {
83-
return new ArrayList<String>();
84-
}
85-
curr = next;
86-
}
52+
while (left < right) {
53+
int mid = left + ((right - left) >> 1) + 1;
8754

88-
PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> (a.c == b.c ? a.s.compareTo(b.s) : b.c - a.c));
89-
for (String s : curr.counts.keySet()) {
90-
pq.add(new Pair(s, curr.counts.get(s)));
55+
if (target < nums[mid]) {
56+
right = mid - 1;
57+
} else {
58+
left = mid;
9159
}
92-
93-
List<String> res = new ArrayList<String>();
94-
for (int i = 0; i < 3 && !pq.isEmpty(); i++) {
95-
res.add(pq.poll().s);
96-
}
97-
return res;
9860
}
61+
62+
return nums[right] == target ? right : -1;
9963
}
10064
}

0 commit comments

Comments
 (0)