簡體   English   中英

Swift 中是否有任何類似於 Python 的 for else 語法的表達式

[英]Is there any expression in Swift that is similar to Python's for else syntax

我正在解決一個算法問題,我同時使用 Python 和 Swift 來解決它。 在 python 中,我可以使用 for else 語法輕松解決它。 但是在 Swift 中,我正在努力尋找一種類似於 Python 的其他語法的方法。

這是算法問題,它可以幫助您了解我在做什么。

給定一個字符串數組 words,找到兩個單詞不共享公共字母的 length(word[i]) * length(word[j]) 的最大值。 您可以假設每個單詞僅包含小寫字母。 如果不存在這兩個詞,則返回 0。

示例 1:給定 ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]

返回 16

這兩個詞可以是“abcw”、“xtfn”。

示例 2:給定 ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]

返回 4

這兩個詞可以是“ab”、“cd”。

示例 3:給定 ["a", "aa", "aaa", "aaaa"]

返回 0

沒有這樣的一對詞。

這是我的兩套代碼。

Python代碼有效。

class Solution(object):
    def maxProduct(self, words):

        maximum = 0
        while words:
            currentWord = set(words[0])
            current_length = len(words[0])
            words = words[1:]

            for ele in words:
                for char in currentWord:
                    if char in ele:
                        break
                else:
                    maximum = max(maximum,current_length*len(ele))
        return maximum

swift 代碼效果不佳。

class Solution
{
    func maxProduct(words: [String]) -> Int
    {
        var input = words
        let length = input.count
        var maximum = 0
        while input.count != 0
        {
            let cur_word = Set(input[0].characters)
            let cur_length = input[0].characters.count
            input = Array(input[1..<length])


            for item in input
            {
                for char in item.characters
                {
                    if cur_word.contains(char)
                    {
                        break
                    }
                }

                // how add a control follow here? if cur_word does not share same character with item, then does the below max statement

                //else
                //{
                    maximum = max(maximum,cur_length*(item.characters.count))
                //}


            }

        }
        return maximum
    }
}

您可以引入一個標志來記錄是否調用了break 聲明

for a in b:
    if c(a):
        break
else:
    d()

是一樣的

found = False
for a in b:
    if c(a):
        found = True
        break
if not found:
    d()

但請注意,您根本不需要for char in item.characters循環中的for char in item.characters ,因為您可以只使用Set.isDisjointWith(_:)方法

if cur_word.isDisjointWith(item.characters) {
    maximum = ...
}

(在 Swift 3 中,此方法重命名為Set.isDisjoint(with:)

我想分享我的答案。 感謝 Kennytm 的幫助。

class Solution
{
    func maxProduct(words: [String]) -> Int
    {
        var input = words
        var length = input.count
        var maximum = 0
        while input.count != 0
        {
            let cur_word = Set(input[0].characters)
            let cur_length = input[0].characters.count
            input = Array(input[1..<length])
            length -= 1

            for item in input
            {
                if cur_word.isDisjointWith(item.characters)
                {
                    maximum = max(maximum, cur_length*(item.characters.count))
                }
            }

        }
        return maximum
    }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM