Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week04 31 #804

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Week_04/id_31/LeetCode_211_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// LeetCode_211_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/16.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

class WordDictionary {
private class TrieNode {
var children: [Character: TrieNode] = [:]
var isEnd = false
}

private let root: TrieNode

init() {
root = TrieNode()
}

func addWord(_ word: String) {
var node = root
for char in word {
if node.children[char] == nil {
node.children[char] = TrieNode()
}
node = node.children[char]!
}
node.isEnd = true
}

func search(_ word: String) -> Bool {
return bfs(self.root, Array(word), 0)
}

private func bfs(_ node: TrieNode, _ chars: [Character], _ nextIndex: Int) -> Bool {
if nextIndex == chars.count {
return node.isEnd
}

for (nextChar, nextNode) in node.children {
if chars[nextIndex] == "." || chars[nextIndex] == nextChar {
if bfs(nextNode, chars, nextIndex + 1) {
return true
}
}
}

return false
}
}
22 changes: 22 additions & 0 deletions Week_04/id_31/LeetCode_746_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// LeetCode_746_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/20.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

class Solution {
func minCostClimbingStairs(_ cost: [Int]) -> Int {
guard cost.count >= 2 else { return 0 }
var dp: [Int: Int] = [:]
dp[0] = cost[0]
dp[1] = cost[1]
for i in 2..<cost.count {
dp[i] = min(dp[i-1]!, dp[i-2]!) + cost[i]
}
return min(dp[cost.count - 1]!, dp[cost.count - 2]!)
}