Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode299. 猜数字游戏 | Bulls and Cows大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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 tha

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.

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. 

Please note that both secret number and friend‘s guess may contain duplicate digits.

Example 1:

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

Output: "1A3B"

Explanation:  bull and  cows. The bull is,the cows are,and 138017.

Example 2:

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

Output: "1A1B"

Explanation: The 1st in friend‘s guess is a bull,the 2nd or 3rd  is a cow.11

Note: You may assume that the secret number and your friend‘s guess only contain digits,and their lengths are always equal.

你正在和你的朋友玩 猜数字(Bulls and Cows)游戏:你写下一个数字让你的朋友猜。每次他猜测后,你给他一个提示,告诉他有多少位数字和确切位置都猜对了(称为“Bulls”,公牛),有多少位数字猜对了但是位置不对(称为“Cows”,奶牛)。你的朋友将会根据提示继续猜,直到猜出秘密数字。

请写出一个根据秘密数字和朋友的猜测数返回提示函数,用 A 表示公牛,用 B 表示奶牛。

请注意秘密数字和朋友的猜测数都可能含有重复数字。

示例 1:

输入: secret = "1807",guess = "7810"

输出: "1A3B"

解释:  公牛和  奶牛。公牛是 ,奶牛是, 和 。138017

示例 2:

输入: secret = "1123",guess = "0111"

输出: "1A1B"

解释: 朋友猜测数中的第一个  是公牛,第二个或第三个  可被视为奶牛。11

说明: 你可以假设秘密数字和朋友的猜测数都只包含数字,并且它们的长度永远相等。

40ms

 1 class Solution {
 2     func getHint(_ secret: String,_ guess: String) -> String {
 3         
 4         var acount = 0
 5         var bcount = 0
 6         
 7         var secArr = Array(secret)
 8         var gueArr = Array(guess)
 9         
10         for i in 0..<secArr.count where secArr[i] == gueArr[i] {
11             acount += 1
12             secArr[i] = "a"
13             gueArr[i] = "b"
14         }
15         
16         secArr = secArr.filter{return $0 != "a"}
17         gueArr = gueArr.filter{return $0 != "b"}
18         
19         for i in 0..<gueArr.count {
20             let gue = gueArr[i]
21             
22             for j in 0..<secArr.count where secArr[j] == gue {
23                 bcount += 1
24                 secArr.remove(at: j)
25                 break
26             }
27             
28         }
29         
30         return "\(acount)A\(bcount)B"
31     }
32 }

大佬总结

以上是大佬教程为你收集整理的[Swift]LeetCode299. 猜数字游戏 | Bulls and Cows全部内容,希望文章能够帮你解决[Swift]LeetCode299. 猜数字游戏 | Bulls and Cows所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。