Tagged in

algorithms

Algorithms part1

Two Pointers

1.Middle of Linked List[E]

Description Find the middle node of a linked list.

Example - Given 1->2->3, return the node with value 2. - Given 1->2, return the node with value 1.

Challenge If the linked list is in a data stream, can you find …

swift algorithms part 1

Algorithms part0

BinarySearch & LogN Alogorithm

  • 模板start+1<end
  • mid= start + (end – start) / 2确保不会越界
  • 有的时候不一定是二分,有可能是扩大搜索量,如1,2,4,8...2^x, 2^(x+1)
  • 如果可以画图可以通过画图的方式来确定问题
  • 可以通过二分的方式来排除一部分答案来确定最后的值

1.LastPositionOfTarget[E]

Description

Find the last position of a target number in a sorted array. Return -1 if target does not exist.

Example …

swift algorithms part 0

Extension

Extension的使用方法

  1. 直接扩展某一个类
extension Int {
    func repeats(word:String)->[String]{
        return [String](repeating: word, count: self)
    }
}
  1. 扩展某一个泛型Type
extension Array where Element: Comparable {
    func linearSearch(forElement key:Element) -> Bool {
        for number in self {
            if number == key {
                return true
            }
        }
        return false
    }
}
  1. 实现类的特定protocol & 扩展struct
struct Dog {
    var name:String
}

extension Dog:Hashable {
    var …