FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Week04 31 by glve1027 · Pull Request #804 · algorithm001/algorithm · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .swift  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
53 changes: 53 additions & 0 deletions Week_04/id_31/LeetCode_211_031.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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]!)
}


Back | FazBrowse Home | New Git URL