[Swift]LeetCode843. 猜猜这个单词 | Guess the Word
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10589944.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
This problem is an interactive problem new to the LeetCode platform.
We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret.
You may call master.guess(word)
to guess a word. The guessed word should have type string
and must be from the original list with 6 lowercase letters.
This function returns an integer
type, representing the number of exact matches (value and position) of your guess to the secret word. Also, if your guess is not in the given wordlist, it will return -1
instead.
For each test case, you have 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or less calls to master.guess
and at least one of these guesses was the secret, you pass the testcase.
Besides the example test case below, there will be 5 additional test cases, each with 100 words in the word list. The letters of each word in those testcases were chosen independently at random from 'a'
to 'z'
, such that every word in the given word lists is unique.
master.guess("aaaaaa")
"aaaaaa"
master.guess("acckzz")
"acckzz"
master.guess("ccbazz")
"ccbazz"
master.guess("eiowzz")
"eiowzz"
master.guess("abcczz")
"abcczz"
Note: Any solutions that attempt to circumvent the judge will result in disqualification.
这个问题是 LeetCode 平台新增的交互式问题 。
我们给出了一个由一些独特的单词组成的单词列表,每个单词都是 6 个字母长,并且这个列表中的一个单词将被选作秘密。
你可以调用 master.guess(word)
来猜单词。你所猜的单词应当是存在于原列表并且由 6 个小写字母组成的类型字符串
。
此函数将会返回一个整型数字
,表示你的猜测与秘密单词的准确匹配(值和位置同时匹配)的数目。此外,如果你的猜测不在给定的单词列表中,它将返回 -1
。
对于每个测试用例,你有 10 次机会来猜出这个单词。当所有调用都结束时,如果您对 master.guess
的调用不超过 10 次,并且至少有一次猜到秘密,那么您将通过该测试用例。
除了下面示例给出的测试用例外,还会有 5 个额外的测试用例,每个单词列表中将会有 100 个单词。这些测试用例中的每个单词的字母都是从 'a'
到 'z'
中随机选取的,并且保证给定单词列表中的每个单词都是唯一的。
master.guess("aaaaaa")
"aaaaaa"
master.guess("acckzz") 返回
"acckzz"
master.guess("ccbazz")
"ccbazz"
master.guess("eiowzz")
"eiowzz"
master.guess("abcczz")
"abcczz"
提示:任何试图绕过评判的解决方案都将导致比赛资格被取消。
1 /** 2 * // This is the Master's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * class Master { 5 * public func guess(word: String) -> Int {} 6 * } 7 */ 8 class Solution { 9 func findSecretWord(_ wordlist: [String], _ master: Master) { 10 var wordlist = wordlist 11 var i:Int = 0 12 var x:Int = 0 13 while(i < 10 && x < 6) 14 { 15 var guess:String = wordlist[Int.random(in:0..<wordlist.count)] 16 var x:Int = master.guess(guess) 17 var wordlist2:[String] = [String]() 18 for w in wordlist 19 { 20 if match(guess, w) == x 21 { 22 wordlist2.append(w) 23 } 24 } 25 wordlist = wordlist2 26 i += 1 27 } 28 } 29 30 func match(_ a:String,_ b:String) -> Int 31 { 32 var matches:Int = 0 33 var arrA:[Character] = Array(a) 34 var arrB:[Character] = Array(b) 35 for i in 0..<a.count 36 { 37 if arrA[i] == arrB[i] 38 { 39 matches += 1 40 } 41 } 42 return matches 43 } 44 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了