【问题标题】:Is there any expression in Swift that is similar to Python's for else syntaxSwift 中有没有类似于 Python 的 for else 语法的表达式
【发布时间】:2017-01-04 04:40:38
【问题描述】:

我正在解决一个算法问题,我同时使用 Python 和 Swift 来解决它。在 python 中,我可以使用 for else 语法轻松解决它。但是在 Swift 中,我正在努力寻找一种类似于 python 的 for else 语法的方法。

这是算法问题,它可以帮助你理解我在做什么。

给定一个字符串数组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
    }
}

【问题讨论】:

    标签: python swift2 for-else


    【解决方案1】:

    你可以引入一个标志来记录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 循环,因为您可以只使用 Set.isDisjointWith(_:) method

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

    (在 Swift 3 上,此方法为 renamed to Set.isDisjoint(with:)

    【讨论】:

      【解决方案2】:

      我想分享我的答案。感谢 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
          }
      } 
      

      【讨论】:

        猜你喜欢
        • 2012-07-19
        • 1970-01-01
        • 2018-01-21
        • 2016-06-17
        • 2011-09-26
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-13
        相关资源
        最近更新 更多