题目如下:

解题思路:本题难度不太大,对时间复杂度也没有很高的要求。我的做法是用一个字典来保存每个字符出现的次数,用正数1记录标记secret中出现的字符,用负数1记录guess中出现的字符,这样每出现一次正负抵消,即表示出现了一次cow。

代码如下:

class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""
dic = {}
bull = 0
cow = 0
for i in xrange(len(secret)):
if secret[i] == guess[i]:
bull += 1
else:
if dic.has_key(secret[i]):
if dic[secret[i]] < 0:
cow += 1
dic[secret[i]] += 1
else:
dic[secret[i]] = 1 if dic.has_key(guess[i]):
if dic[guess[i]] > 0:
cow += 1
dic[guess[i]] -= 1
else:
dic[guess[i]] = -1 if dic[guess[i]] == 0:
del dic[guess[i]]
if dic[secret[i]] == 0:
del dic[secret[i]] return str(bull) + 'A' + str(cow) + 'B'

【leetcode】299. Bulls and Cows的更多相关文章

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

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

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

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

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

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

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

    class Solution(object):    def getHint(self, secret, guess):        """        :type ...

  5. 299. Bulls and Cows - LeetCode

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

  6. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  7. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. lgb参数及调参

    1 参数含义 max_depth: 设置树的最大深度,默认为-1,即不限制最大深度,它用于限制过拟合 num_leave: 单颗树的叶子数目,默认为31 eval_metric: 评价指标,可以用lg ...

  2. Cloudera-JDBC-Driver-for-Impala

    Cloudera-JDBC-Driver-for-Impala-Install-Guide-2-5-5.pdf https://github.com/FlowerBirds/flowerbirds.g ...

  3. oracle dis系列课程总结

    oracle dis系列课程总结 1 bbed安装和介绍 --1 bbed的安装--(Oracle Block Brower and EDitor Tool) 2 controlfile 丢失的恢复 ...

  4. linux--vm安装

    网络排错图解 https://www.linuxidc.com/Linux/2017-03/141863.htm net模式 https://www.linuxidc.com/Linux/2017-0 ...

  5. Python 内置函数super

    super()函数是用于调用父类/超类的一个方法 super是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没有问题,但是如果使用多继承,会涉及到查找顺序(MRO),重复调用(钻石继 ...

  6. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  7. Java利用Base64编码和解码图片文件

    1.编码与解码代码如下所示: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import jav ...

  8. Kotlin学习(5)类型系统

    可空性(避免空指针异常) /* *这个函数的参数代表传入一个String类型变量的实例,这代表它不可以为空 */ fun a(str:String){ println(str) } //这样调用a() ...

  9. SCUT - 485 - 质因数计数 - 原根

    https://scut.online/p/485 给定a和n,求有多少个质数p,满足n是使得a^n=1 mod p成立的最小正整数. 翻译:求有多少个质数p,使得a模p的阶delta_m(a)是n ...

  10. N皇后问题(递归)

    //八皇后递归解法 //#include<iostream> //using namespace std; #include<stdio.h> ] = {-,-,-,-,-,- ...