★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-hmwleknf-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
This problem is an interactive problem new to the LeetCode platform.git
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.github
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.app
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.less
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.dom
Example 1: Input: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"] Explanation: returns -1, because is not in wordlist. returns 6, because is secret and has all 6 matches. returns 3, because has 3 matches. returns 2, because has 2 matches. returns 4, because has 4 matches. We made 5 calls to master.guess and one of them was the secret, so we pass the test case. 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.ide
這個問題是 LeetCode 平臺新增的交互式問題 。函數
咱們給出了一個由一些獨特的單詞組成的單詞列表,每一個單詞都是 6 個字母長,而且這個列表中的一個單詞將被選做祕密。測試
你能夠調用 master.guess(word)
來猜單詞。你所猜的單詞應當是存在於原列表而且由 6 個小寫字母組成的類型字符串
。
此函數將會返回一個整型數字
,表示你的猜想與祕密單詞的準確匹配(值和位置同時匹配)的數目。此外,若是你的猜想不在給定的單詞列表中,它將返回 -1
。
對於每一個測試用例,你有 10 次機會來猜出這個單詞。當全部調用都結束時,若是您對 master.guess
的調用不超過 10 次,而且至少有一次猜到祕密,那麼您將經過該測試用例。
除了下面示例給出的測試用例外,還會有 5 個額外的測試用例,每一個單詞列表中將會有 100 個單詞。這些測試用例中的每一個單詞的字母都是從 'a'
到 'z'
中隨機選取的,而且保證給定單詞列表中的每一個單詞都是惟一的。
示例 1: 輸入: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"] 解釋: 返回 -1, 由於 不在 wordlist 中. 6, 由於 就是祕密,6個字母徹底匹配。 返回 3, 由於 有 3 個匹配項。 返回 2, 由於 有 2 個匹配項。 返回 4, 由於 有 4 個匹配項。 咱們調用了 5 次master.guess,其中一次猜到了祕密,因此咱們經過了這個測試用例。 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 }