在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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 This function returns an 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 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 Example 1: Input: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"] Explanation: Note: Any solutions that attempt to circumvent the judge will result in disqualification. 这个问题是 LeetCode 平台新增的交互式问题 。 我们给出了一个由一些独特的单词组成的单词列表,每个单词都是 6 个字母长,并且这个列表中的一个单词将被选作秘密。 你可以调用 此函数将会返回一个 对于每个测试用例,你有 10 次机会来猜出这个单词。当所有调用都结束时,如果您对 除了下面示例给出的测试用例外,还会有 5 个额外的测试用例,每个单词列表中将会有 100 个单词。这些测试用例中的每个单词的字母都是从 示例 1: 输入: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"] 解释: 提示:任何试图绕过评判的解决方案都将导致比赛资格被取消。 Runtime: 8 ms
Memory Usage: 19.1 MB
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 }
|
请发表评论