Skip to content

Commit 0d35982

Browse files
committed
new problem
1 parent 0a0e27c commit 0d35982

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)| [Java](leetcode/solution/src/FindMedianFromDataStream.java)||
270270
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[Java](leetcode/solution/src/Codec.java)|80|好题目,多做做,递归的非递归的|
271271
|298|[Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/)| [Java](leetcode/solution/src/BinaryTreeLongestConsecutiveSequence.java)|70|多做三遍,尽可能简洁|
272+
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Java](leetcode/solution/src/BullsAndCows.java)||
272273
|300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Java](leetcode/solution/src/LongestIncreasingSubsequence.java)||
273274
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Java](leetcode/solution/src/RemoveInvalidParentheses.java)|70|比较难,多做几遍|
274275
|302|[Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/)| [Java](leetcode/solution/src/SmallestRectangleEnclosingBlackPixels.java)|||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
5+
public class BullsAndCows {
6+
7+
public String getHint(String secret, String guess) {
8+
HashMap<Character, Set<Integer>> map1 = new HashMap<>();
9+
HashMap<Character, Set<Integer>> map2 = new HashMap<>();
10+
for (int i = 0; i < secret.length(); i++) {
11+
Set<Integer> set = map1.getOrDefault(secret.charAt(i), new HashSet<>());
12+
set.add(i);
13+
map1.put(secret.charAt(i), set);
14+
}
15+
for (int i = 0; i < guess.length(); i++) {
16+
Set<Integer> set = map2.getOrDefault(guess.charAt(i), new HashSet<>());
17+
set.add(i);
18+
map2.put(guess.charAt(i), set);
19+
}
20+
int bulls = 0, cows = 0;
21+
for (Character c : map2.keySet()) {
22+
Set<Integer> set1 = map1.get(c);
23+
24+
if (set1 == null) {
25+
continue;
26+
}
27+
28+
Set<Integer> set2 = map2.get(c);
29+
30+
int count = 0;
31+
32+
for (Integer index : set2) {
33+
if (set1.contains(index)) {
34+
count++;
35+
}
36+
}
37+
38+
bulls += count;
39+
cows += Math.min(set1.size(), set2.size()) - count;
40+
}
41+
42+
return String.format("%dA%dB", bulls, cows);
43+
}
44+
}

leetcode/src/Main.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,48 @@ public class Main {
66

77
public static class Solution {
88

9-
public interface Master {
10-
int guess(String word);
11-
}
9+
public String getHint(String secret, String guess) {
10+
HashMap<Character, Set<Integer>> map1 = new HashMap<>();
11+
HashMap<Character, Set<Integer>> map2 = new HashMap<>();
12+
for (int i = 0; i < secret.length(); i++) {
13+
Set<Integer> set = map1.getOrDefault(secret.charAt(i), new HashSet<>());
14+
set.add(i);
15+
map1.put(secret.charAt(i), set);
16+
}
17+
for (int i = 0; i < guess.length(); i++) {
18+
Set<Integer> set = map2.getOrDefault(guess.charAt(i), new HashSet<>());
19+
set.add(i);
20+
map2.put(guess.charAt(i), set);
21+
}
22+
int bulls = 0, cows = 0;
23+
for (Character c : map2.keySet()) {
24+
Set<Integer> set1 = map1.get(c);
25+
26+
if (set1 == null) {
27+
continue;
28+
}
29+
30+
Set<Integer> set2 = map2.get(c);
1231

32+
int count = 0;
1333

14-
public void findSecretWord(String[] wordlist, Master master) {
15-
Random random = new Random();
16-
for (int i = 0; i < 10; i++) {
17-
String word = wordlist[random.nextInt(wordlist.length)];
18-
int match = master.guess(word);
19-
List<String> list = new ArrayList<>();
20-
for (String s : wordlist) {
21-
if (match(s, word) == match) {
22-
list.add(s);
34+
for (Integer index : set2) {
35+
if (set1.contains(index)) {
36+
count++;
2337
}
2438
}
25-
wordlist = list.toArray(new String[0]);
26-
}
27-
}
2839

29-
private int match(String s, String t) {
30-
int match = 0;
31-
for (int i = 0; i < s.length(); i++) {
32-
if (s.charAt(i) == t.charAt(i)) {
33-
match++;
34-
}
40+
bulls += count;
41+
cows += Math.min(set1.size(), set2.size()) - count;
3542
}
36-
return match;
43+
44+
return String.format("%dA%dB", bulls, cows);
3745
}
3846
}
3947

4048
public static void main(String[] args) {
4149
Solution solution = new Solution();
42-
43-
double s = solution.mincostToHireWorkers(new int[] {
44-
3,1,10,10,1
45-
}, new int[] {
46-
4,8,2,2,7
47-
},3);
48-
50+
String s = solution.getHint("1123", "0111");
4951
System.out.println(s);
5052
}
5153
}

0 commit comments

Comments
 (0)