iOS 大神之路iOS奋斗SwiftBlog

Swift 算法实战之路:基本语法与技巧

2016-05-06  本文已影响5249人  故胤道长

Swift是苹果新推出的编程语言,也是苹果首个开源语言。相比于原来的Objective-C,Swift要更轻便和灵活。笔者最近使用Swift实践了大量的算法(绝大部分是硅谷各大公司的面试题),将心得体会总结于下。此文并不是纯粹讨论Swift如何实现某一个具体的算法或者数据结构,如冒泡排序、深度优先遍历,或是树和栈,而是总结归纳一些Swift常用的语法和技巧,以便大家在解决面试题中使用。

基本语法

先来看下面一段代码

func swap(chars:[Character],  p: Int, q: Int) {
  var temp = chars[p]
  chars[p] = chars[q]
  chars[q] = temp
}
// Assume array is a character array and it has enough elements
swap(array, p: 0, q: 1)

上面代码实现了一个非常简单的功能,就是交换一个数组中的两个值。乍一看非常正确,实际上存在以下几个问题。

  1. 在第一个参数的类型前应该加上 inout关键字。因为在Swift中,struct都是按值传递,class是按引用传递;数组和字典都是struct。所以要改变原来的chars数组,在其前部加入inout关键字,表示是按引用传递。
  2. p, q, 和chars 之前应该加入下划线。Swift 3.0 默认函数的所有数都是外部(external)变量,外部变量必须声明。如果在外部变量前加上下划线,则在调用函数时无需声明外部变量,这样调用起来更简便。
  3. temp前的var应该改为let。let用于声明常量(constant),var用于声明变量(variable)。swap函数中,temp并未进行修改,所以应当视为常量,用let来声明。

修改过后的代码为

func swap(chars: inout [Character],  _ p: Int, _ q: Int) {
  let temp = chars[p]
  chars[p] = chars[q]
  chars[q] = temp
}
// Assume array is a character array and it has enough elements
swap(&array, 0, 1)

更进一步,我们可以使用泛型编程,并配合Tuple,在配合上断言,就写出了灵活度很高的交换方法。代码如下:

func swap<T>(_ array: inout [T],  _ p: Int, _ q: Int) {
  assert(p >= 0 && p < array.count)
  assert(q >= 0 && q < array.count)
  
  (array[p], array[q]) = (array[q], array[p])
}

再来看一段代码

func toZero(x: Int) -> Int {
  while x > 0 {
    x -= 1
  }
  return x
}

这里在 x -= 1 处会报错。原因是函数中所有的参数是常量(let),是不可以修改的。解决的方法是在函数中写上var x = x,之后就可以对 x 进行修改了

循环

Swift循环分为for和while两种,注意他们的结构跟传统的 Java, C/C++有很大区别,笔者将其总结如下

// Assume names is an array holds enough Strings
// for loop
for (index, element) in names.enumerated()
for name in names { }
names.forEach { element in }
for i in 0 ... names.count - 1 { }
for i in 0 ..< names.count { }
for _ in 0 ..< names.count { }
for name in names.reverse() { }
for i in 0.stride(through: names.count - 1, by 2) { }
for i in 0.stride(to: names.count, by: 2) { }

// while loop
var i = 0
while i < names.count { }
repeat { } while i < names.count

以上代码非常简单。需要说明的有两个,一个是 for _ in 0 ..< names.count { } 。当我们不需要数组中每一个具体的元素值时,我们就可以用下划线来代表序号。
另一个是是 repeat { } while i < names.count 。这个相当于我们熟悉(java)的 do { } while (i < names.length)
注意,在循环中,i是let变量,是不可以修改的。

排序

swift排序效率很高,写法也很简洁。笔者将其总结如下

// Sort an Int array,ascending
nums.sort()

// Sort an Int array without mutating the original one, ascending
var sortedNums = nums.sorted()

// Sort an array with custom-defined objects, ascending
timeIntervals.sort { $0.startTime < $1.startTime }

// Sort keys in a dictionary according to its value, ascending
let keys = Array(dict.keys)
var sortedKeys = keys.sorted {
  let value0 = dict[$0]!
  let value1 = dict[$1]!
  return value0 < value1
}

活用Guard语句

使用Guard语句可以让逻辑变得非常清楚,尤其是在处理算法问题的时候,请看下面的代码

// Returns the index of the first occurrence of needle in haystack, 
// or -1 if needle is not part of haystack
func strStr(haystack: String, _ needle: String) -> Int {
  let hCount = haystack.count, nCount = needle.count
  if hCount == 0 || hCount < nCount {
    return -1
  }
  
  for i in 0...nCount - nCount {
    if haystack[i] == needle[0] {
      for j in 0..<needle.count {
        if haystack[i + j] != needle[j] {
          break
        } else {
          if j == needle.count - 1 {
            return i
          }
        }
      }
    }
  }  
  return -1
}

extension String {
  subscript(i: Int) -> Character {
    return self[index(startIndex, offsetBy: i)]
  }
}

上面这段代码是求字符串needle在字符串haystack里首次出现的位置。我们发现for循环中的嵌套非常繁琐,但是如果使用Guard语句代码会变得清晰很多:

func strStr(haystack: String, _ needle: String) -> Int {
  guard haystack.count != 0 && haystack.count >= needle.count else {
    return -1
  }
  
  for i in 0...haystack.count - needle.count {
    guard haystack[i] == needle[0] else {
      continue
    }
    
    for j in 0 ..< needle.count {
      guard haystack[i + j] == needle[j] else {
        break
      }
      if j == needle.count - 1 {
        return i
      }
    }
  }
  
  return -1
}

Guard语句的好处是判断条件永远是我们希望的条件而不是特殊情况,且成功避免了大量的if嵌套。

另外在上面代码中,笔者给字符串添加了下标实现(subscript)。这里用到的index(startIndex, offsetBy: i)方法,其时间复杂度为O(n)。所以如果考虑性能最优,最好将字符串转化为字符数组进行处理。

总结

Swift是一门独特的语言,本文对其基本知识和语法进行了适当归纳,文中提到的都是基本细节。下期我们会讨论更加进阶的内容。

上一篇下一篇

猜你喜欢

热点阅读