一、问题描述:

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  01  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.

二、解决思路:

bulls :  首先遍历找到对应位置相同的,如果相同bulls 加1;借助一个布尔型的二维数组,位置相同的地方置为false, 在计算cows的时候不参与计算。 时间为N

cows : 因为是猜数字,用两个长度为10的二维数组来存放secrect 和guess中 每个数字出现的次数, 遍历两个数组每次取最小值叠加cows                      时间为3N

三、代码:

public String getHint(String secret, String guess) {int bows = 0;int cows = 0;boolean[] flag = new boolean[secret.length()];for (int i = 0; i < secret.length(); i++) {flag[i] = true;if (secret.charAt(i) == guess.charAt(i)) {bows++;flag[i] = false;}}int[] sec = new int[10];int[] gue = new int[10];for (int i = 0; i < secret.length(); i++) {if (flag[i]) {char tmpsec = secret.charAt(i);char tmpgue = guess.charAt(i);sec[tmpsec - 48]++;gue[tmpgue - 48]++;}}for (int i = 0; i < 10; i++) {cows += Math.min(sec[i], gue[i]);}return bows + "A" + cows + "B";}

Tips : char  类型的可以直接用ascii 码值计算。

leetcode oj java Bulls and Cows相关推荐

  1. leetcode算法题--Bulls and Cows

    原题链接:https://leetcode.com/problems/bulls-and-cows/ string getHint(string secret, string guess) {int ...

  2. LeetCode 299. Bulls and Cows(公牛和母牛)

    原题网址:https://leetcode.com/problems/bulls-and-cows/ You are playing the following Bulls and Cows game ...

  3. 299. Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  4. 299 Bulls and Cows 猜数字游戏

    你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls", ...

  5. leetcode 46 java,leetcode46.java

    package leetcode; import java.util.ArrayList; import java.util.List; /** * Created by 林剑 on 2016/10/ ...

  6. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  7. Leetcode算法Java全解答--37. 解数独

    Leetcode算法Java全解答–37. 解数独 文章目录 Leetcode算法Java全解答--37. 解数独 题目 想法 结果 总结 代码 我的答案 大佬们的答案 测试用例 其他 题目 编写一个 ...

  8. 猜数字(Bulls and Cows)游戏

    你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为"Bulls", ...

  9. Leetcode算法Java全解答--19. 删除链表的倒数第N个节点

    Leetcode算法Java全解答–19. 删除链表的倒数第N个节点 文章目录 Leetcode算法Java全解答--19. 删除链表的倒数第N个节点 题目 想法 结果 总结 代码 我的答案 大佬们的 ...

最新文章

  1. 显卡不够时,如何训练大型网络
  2. Problem 5 素数筛法+并查集
  3. Android API之android.net.wifi.WifiConfiguration
  4. c语言给bmp图片加滤镜,关于BMP位图透明通道的详解制作教程, 教你输出透明的BMP位图...
  5. MemDC,GDI绘制注意点
  6. 权限丢失_应用好深圳RFID智能档案密集架,杜绝档案丢失!
  7. android studio for android learning (十四) android的数据的存储sharedPreferences
  8. 学校后勤物资管理系统(数据库课程设计)
  9. SPSS实战:多因素方差分析(以随机区组设计为例)
  10. 硬核干货 | 人脸识别的原理是什么?
  11. android studio 添加 ignored files 忽略文件
  12. 小组取什么名字好_注册公司取什么名字好 八字起名大全
  13. 武汉理工大学数据库综合实验——教学管理信息系统--本科专业培养计划
  14. 正好在线炒股医美概念涨幅居前
  15. 东北四省赛E-Minimum Spanning Tree-贡献求和
  16. 北大哲学系杨立华先生
  17. OTP动态密码_Java代码实现
  18. 转载一篇不错的文章:谈谈“野生”Java程序员学习的道路
  19. python中做为词云轮廓的图片,Python如何生成词云图片
  20. 英语 —语法— 句子成分

热门文章

  1. Python世界里的魔术方法(一)
  2. PHY--PUSCH
  3. golang go-redis和redigo的不同用法
  4. 如何书写一手优雅的代码之小刚有话说
  5. 英语学习者的十句经典名言
  6. 开关电源matlab仿真文件,《MATLAB+在开关电源仿真中的应用.pdf》-支持高清全文免费浏览-max文档...
  7. coreapi自动生成API接口文档
  8. c++跟踪调试怎么用 dev_DEVC++调试方法
  9. 神技能:Python控制键盘鼠标
  10. 【jquery】Chosen.jquery.js 插件动态加载数据问题