[LC] 299. Bulls and Cows
Example 1:
Input: secret = "1807", guess = "7810" Output: "1A3B" Explanation:1
bull and3
cows. The bull is8
, the cows are0
,1
and7.
Example 2:
Input: secret = "1123", guess = "0111" Output: "1A1B" Explanation: The 1st1
in friend's guess is a bull, the 2nd or 3rd1
is a cow.
class Solution {
public String getHint(String secret, String guess) {
int[] nums = new int[10];
int len = secret.length();
int bulls = 0;
int cows = 0;
for (int i = 0; i < len; i++) {
char sWord = secret.charAt(i);
char gWord = guess.charAt(i);
if (sWord == gWord) {
bulls += 1;
} else {
if (nums[gWord - '0'] > 0) {
cows += 1;
}
if (nums[sWord - '0'] < 0) {
cows += 1;
}
nums[sWord - '0'] += 1;
nums[gWord - '0'] -= 1;
}
}
return bulls + "A" + cows + "B";
}
}
[LC] 299. Bulls and Cows的更多相关文章
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 299. Bulls and Cows - LeetCode
Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- 299. Bulls and Cows
题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...
- 【一天一道LeetCode】#299. Bulls and Cows
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- [leetcode]299. Bulls and Cows公牛和母牛
You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...
- 299 Bulls and Cows 猜数字游戏
你正在和你的朋友玩猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜.每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为”Bulls“, 公牛),有多少位数字 ...
- Leetcode 299 Bulls and Cows 字符串处理 统计
A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...
- 【leetcode❤python】 299. Bulls and Cows
#-*- coding: UTF-8 -*-class Solution(object): def getHint(self, secret, guess): " ...
随机推荐
- python函数-装饰器
python函数-装饰器 1.装饰器的原则--开放封闭原则 开放:对于添加新功能是开放的 封闭:对于修改原功能是封闭的 2.装饰器的作用 在不更改原函数调用方式的前提下对原函数添加新功能 3.装饰器的 ...
- 求1+2+3+…..+n
[问题]求1+2+3+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). [思路]由于题目好多运算符不能用,我们只有想到使用递 ...
- UML-架构分析-阶段
初始阶段:架构概念验证原型--->确定其可行性 细化阶段:因素表.技术备忘录.SAD(软件架构文档) 移交阶段:可能会修改SAD->确保与最终部署版本的一致性 后续进化循环:重温架构性因素 ...
- html_js_jq_css
// ----- JQ $(function(){$(div').bind('mouseout mouseover', function () {// 移入和移出分别执行一次alert('bind 可 ...
- priority_queue(优先队列)的用法(包括pbds)
置顶!!! 有时候在定义的时候,不要把两个<>连在一起写,以免被编译器错误理解!!!! 头文件 #include <queue> queue的一般用法不再叙述 类型名 prio ...
- 基于Token的身份验证
最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...
- PHP时间戳常用转换
//设置中国时区 date_default_timezone_set('PRC'); //今天的时间搓 $today_start = strtotime(date('Y-m-d',time()).' ...
- LeetCode——456.132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- KVM---利用 libvirt+qemu-kvm 创建虚拟机
KVM 虚拟化已经是一个工业级的虚拟化解决方案了,以前都是直接下载 VMware,然后安装其他操作系统的,今天我们来体验一下自己动手创建一台虚拟机,这样你就会知道在KVM下创建一台虚拟机,是多么简单的 ...
- PAT Advanced 1134 Vertex Cover (25) [hash散列]
题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...