leetcode 299. Bulls and Cows 猜数字游戏 + 直接统计 + 很棒的做法

àì夳堔傛蜴生んèń 2022-06-08 14:37 287阅读 0赞

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:

Secret number: “1807”
Friend’s guess: “7810”
Hint: 1 bull and 3 cows. (The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.

Please note that both secret number and friend’s guess may contain duplicate digits, for example:

Secret number: “1123”
Friend’s guess: “0111”
In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.

这道题很简单,比如如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了,记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。接着猜的人再根据出题者的几A几B继续猜,直到猜中(即 4A0B)为止。

最简单的做法就是遍历 + 排序,然后遍历求解,不过比较费时。 我在网上看到了一个很不错的做法,使用一个flag计数数组来实现数字的配对,很不错。

要学会这种统计方法,真的很棒

代码如下:

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. /*
  6. * 这里是一个更好的解法,因为我们要猜的数据都是数组,所以这里设置了一个flag数组
  7. * 初始化为0,通过加加减减来实现配对
  8. * 很棒的想法
  9. * */
  10. class Solution
  11. {
  12. public String getHint(String secret, String guess)
  13. {
  14. if(secret==null || guess==null || secret.length()<=0 || guess.length()<=0)
  15. return "0A0B";
  16. int bull=0 , cow=0;
  17. int []flag=new int[10];
  18. Arrays.fill(flag, 0);
  19. for(int i=0;i<secret.length();i++)
  20. {
  21. int a=secret.charAt(i)-'0';
  22. int b=guess.charAt(i)-'0';
  23. if(a==b)
  24. bull++;
  25. else
  26. {
  27. if(flag[a]<0)
  28. cow++;
  29. if(flag[b]>0)
  30. cow++;
  31. flag[a]++;
  32. flag[b]--;
  33. }
  34. }
  35. return bull + "A" + cow + "B";
  36. }
  37. /*
  38. * 我这使用最傻逼的方法求解的:也即排序
  39. * */
  40. public String getHint1(String secret, String guess)
  41. {
  42. if(secret==null || guess==null || secret.length()<=0 || guess.length()<=0)
  43. return "0A0B";
  44. int bull=0 , cow=0;
  45. List<Integer> ss=new ArrayList<>();
  46. List<Integer> gg=new ArrayList<>();
  47. for(int i=0;i<secret.length();i++)
  48. {
  49. if(secret.charAt(i)==guess.charAt(i))
  50. bull++;
  51. else
  52. {
  53. ss.add(secret.charAt(i)-'0');
  54. gg.add(guess.charAt(i)-'0');
  55. }
  56. }
  57. Collections.sort(ss);
  58. Collections.sort(gg);
  59. int i=0,j=0;
  60. while(i<ss.size() && j<gg.size())
  61. {
  62. if(ss.get(i)==gg.get(j))
  63. {
  64. cow++;
  65. i++;
  66. j++;
  67. }else if(ss.get(i)<gg.get(j))
  68. i++;
  69. else
  70. j++;
  71. }
  72. return bull+"A"+cow+"B";
  73. }
  74. }

下面是C++的做法,就是一个很简单的替换处理

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <stack>
  7. #include <string>
  8. #include <climits>
  9. #include <algorithm>
  10. #include <sstream>
  11. using namespace std;
  12. class Solution
  13. {
  14. public:
  15. string getHint(string s, string g)
  16. {
  17. int bull = 0, row = 0;
  18. vector<int> flag(10,0);
  19. for (int i = 0; i < s.length(); i++)
  20. {
  21. int a = s[i] - '0';
  22. int b = g[i] - '0';
  23. if (a == b)
  24. bull++;
  25. else
  26. {
  27. if (flag[a] < 0)
  28. row++;
  29. if (flag[b] > 0)
  30. row++;
  31. flag[a]++;
  32. flag[b]--;
  33. }
  34. }
  35. return to_string(bull) + "A" + to_string(row) + "B";
  36. }
  37. };

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode299. 数字游戏

    你正在和你的朋友玩 [猜数字(Bulls and Cows)][Bulls and Cows]游戏:你写下一个数字让你的朋友猜。每次他猜测后,你给他一个提示,告诉他有多少位数字