LeetCode算法自刷

99 阅读1分钟

Demo: github.com/DMDavid/Alg…

面试题 01.01. 判定字符是否唯一
234. 回文链表

面试题 01.01. 判定字符是否唯一

实现一个算法,确定一个字符串 s 的所有字符是否全都不同。

示例 1:

输入: s = "leetcode" 输出: false 示例 2:

输入: s = "abc" 输出: true

第一反应的解法:遍历

    let strList = Array(astr)

    for index in 0..<strList.count {
        let oneStr = strList[index]

        for (insideIndex, str) in strList.enumerated() {
            if insideIndex == index { continue }
            if str == oneStr {
                return false
            }
        }
    }
    return true

优化思路代码: 使用链表

func isUnique(_ astr: String) -> Bool {
    let strList: [String.Element] = Array(astr)
    var maps: [String: Int] = [:]
    
    for item in strList {
        
        var count = 0
        if var value = maps[String(item)] {
            value += 1
            count = value
            if count > 1 {
                return false
            }
        }
        maps[String(item)] = count
    }
    
    return true
}

234. 回文链表

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2 输出: false 示例 2:

输入: 1->2->2->1 输出: true 进阶: 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

解题思路:

  1. 遍历出来所有的value,然后从中间截取,反转一个数组,两个list 作比较
  2. 用快慢指针,然后比较

One

public class ListNode {
    public var val: Int
    public var next: ListNode?
    public init(_ val: Int) {
        self.val = val
        self.next = nil
    }
}


class Solution {
    func isPalindrome(_ head: ListNode?) -> Bool {
        guard let testHeader = head else { return true }
        
        var list:[Int] = []
        var temHeader: ListNode?
        
        temHeader = testHeader
        list.append(testHeader.val)
        
        while temHeader != nil {
            temHeader = temHeader?.next
            if let value = temHeader?.val {
                list.append(value)
            }
        }
        
        var intermediate = list.count / 2
        guard intermediate < list.count, intermediate >= 0 else { return false }
        let firstList: [Int] = Array(list.prefix(intermediate))
        var lastList: [Int] = Array(list.suffix(intermediate))
        
        let newLastList = Array(lastList.reversed())
        
        if firstList.count != newLastList.count, firstList != newLastList { return false }
        for (index, item) in firstList.enumerated() {
            let value = newLastList[index]
            if value != item {
                return false
            }
        }
        
        return true
    }

two

func isPalindrome(_ l1: ListNode?) -> Bool {
    if l1 == nil || l1?.next == nil {
        return false
    }
    
    var fast = l1
    var slow = l1
    var nodeArr:[Int] = [Int]()
    nodeArr.append(l1!.val)
    while fast?.next != nil && fast?.next?.next != nil {
        slow = slow?.next
        fast = fast?.next?.next
        nodeArr.append(slow!.val)
    }
    
    while slow?.next != nil {
        if nodeArr.count != 0 {
            if nodeArr.removeLast() != slow?.next?.val {
                return false
            }
        } else {
            return false
        }
        slow = slow?.next
    }
    return true
}