Example 1:

Input: secret = "1807", guess = "7810"

Output: "1A3B"

Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7.

Example 2:

Input: secret = "1123", guess = "0111"

Output: "1A1B"

Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 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的更多相关文章

  1. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  2. 299. Bulls and Cows - LeetCode

    Question 299. Bulls and Cows Solution 题目大意:有一串隐藏的号码,另一个人会猜一串号码(数目相同),如果号码数字与位置都对了,给一个bull,数字对但位置不对给一 ...

  3. LeetCode 299 Bulls and Cows

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

  4. 299. Bulls and Cows

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

  5. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  6. [leetcode]299. Bulls and Cows公牛和母牛

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

  7. 299 Bulls and Cows 猜数字游戏

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

  8. Leetcode 299 Bulls and Cows 字符串处理 统计

    A就是统计猜对的同位同字符的个数 B就是统计统计猜对的不同位同字符的个数 非常简单的题 class Solution { public: string getHint(string secret, s ...

  9. 【leetcode❤python】 299. Bulls and Cows

    #-*- coding: UTF-8 -*-class Solution(object):      def getHint(self, secret, guess):          " ...

随机推荐

  1. Spring Boot Actuator Endpoints

    常用内建的Endpoints: beans:显示当前Spring应用上下文的Spring Bean完整列表(包含所有ApplicationContext的层次) conditions:显示当前应用所有 ...

  2. BUUCTF-WEB-easy_tornado

    知识点: Python Web 框架:Tornado python中的一个渲染函数:render: 渲染变量到模板中,即可以通过传递不同的参数形成不同的页面. 1. render方法的实质就是生成te ...

  3. 从AppleWatch4发布后对手股价大跌看可穿戴市场未来

    万众瞩目的苹果秋季发布会终于落下了帷幕,这场发布会既有惊喜,也有遗憾.遗憾的是新款iPad Pro.廉价版Macbook air没有亮相.iPhone系列价格较贵等,惊喜的则是iPhone的处理器依然 ...

  4. 施魔法(DP)

    链接:https://ac.nowcoder.com/acm/contest/3003/H来源:牛客网 题目描述 牛可乐有 n 个元素( 编号 1..n ),第 i 个元素的能量值为 ai​. 牛可乐 ...

  5. FZU_1683 矩阵快速幂 求和

    这个题目确实是很简单的一个矩阵快速幂,但是我在求和的时候,用的是标准的求和,即,一共计算logN次Ak,但是这样会超时. 后来就发现原来本身和Sn=Sn-1+Fn:即Sn本身可以写在矩阵当中,所以直接 ...

  6. VUE 引用公共样式

    首先创建公共样式文件 在main.js中引用样式 浏览器效果图

  7. PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

    题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...

  8. Python笔记_第四篇_高阶编程_正则表达式_1.正则表达式简介(re模块)

    1. 从一个判断手机号的问题引入: 如果给你一个字符串,去判断是否是一个手机号码,我们通过之前的学习可以有如下代码: # 如果用普通的方式去检验一个电话号码非常麻烦. def checkPhone(s ...

  9. python爬虫--cookie反爬处理

    Cookies的处理 作用 保存客户端的相关状态 在爬虫中如果遇到了cookie的反爬如何处理? 手动处理 在抓包工具中捕获cookie,将其封装在headers中 应用场景:cookie没有有效时长 ...

  10. IOC与AOP的理解

    转自 https://blog.csdn.net/qq_38006047/article/details/80797386 1,理解“控制反转” 控制反转,也叫依赖注入,是面向对象编程中的一种设计理念 ...