-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c8b27
commit 65961af
Showing
18 changed files
with
365 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 @@ | ||
// | ||
// Solution104.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 104. 二叉树的最大深度 | ||
// https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ | ||
|
||
class Solution104 { | ||
func maxDepth(_ root: TreeNode?) -> Int { | ||
guard let root = root else { return 0 } | ||
|
||
let leftDepth = maxDepth(root.left) | ||
let rightDepth = maxDepth(root.right) | ||
return max(leftDepth,rightDepth) + 1 | ||
} | ||
} |
This file contains 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,26 @@ | ||
// | ||
// Solution1078.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 1078. Bigram 分词 | ||
// https://leetcode-cn.com/problems/occurrences-after-bigram/ | ||
|
||
class Solution1078 { | ||
func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] { | ||
let components = text.components(separatedBy: " ") | ||
var results = [String]() | ||
|
||
for i in 0..<components.count - 2 { | ||
if components[i] == first && components[i+1] == second { | ||
results.append(components[i+2]) | ||
} | ||
} | ||
return results | ||
} | ||
} |
This file contains 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,31 @@ | ||
// | ||
// Solution114.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 114. 二叉树展开为链表 | ||
// https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/submissions/ | ||
|
||
class Solution114 { | ||
func flatten(_ root: TreeNode?) { | ||
|
||
var node = root | ||
while node != nil { | ||
if node?.left != nil { | ||
var tree = node?.left | ||
while tree?.right != nil { | ||
tree = tree?.right | ||
} | ||
tree?.right = node?.right | ||
node?.right = node?.left | ||
node?.left = nil | ||
} | ||
node = node?.right | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -10,5 +10,5 @@ import Foundation | |
|
||
class Solution123 { | ||
|
||
|
||
#warning("123") | ||
} |
This file contains 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
This file contains 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
This file contains 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,31 @@ | ||
// | ||
// Solution20.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 20. 有效的括号 | ||
// https://leetcode-cn.com/problems/valid-parentheses/ | ||
|
||
class Solution20 { | ||
|
||
func isValid(_ s: String) -> Bool { | ||
var stacks = [String]() | ||
for c in s { | ||
if c == "(" { | ||
stacks.append(")") | ||
}else if c == "[" { | ||
stacks.append("]") | ||
}else if c == "{" { | ||
stacks.append("}") | ||
}else if stacks.isEmpty || String(c) != stacks.removeLast() { | ||
return false | ||
} | ||
} | ||
return stacks.isEmpty | ||
} | ||
} |
This file contains 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,34 @@ | ||
// | ||
// Solution22.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class Solution22 { | ||
|
||
func generateParenthesis(_ n: Int) -> [String] { | ||
|
||
var results = [String]() | ||
func recursive(left: Int,right: Int, current: String) { | ||
if right > 0 { | ||
if left > 0 { | ||
recursive(left: left - 1, right: right, current: current + "(") | ||
} | ||
if right > left { | ||
recursive(left:left, right: right - 1, current: current + ")") | ||
} | ||
} else { | ||
results.append(current) | ||
} | ||
} | ||
|
||
recursive(left: n, right: n, current: "") | ||
|
||
return results | ||
} | ||
|
||
} |
This file contains 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,24 @@ | ||
// | ||
// Solution226.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 226. 翻转二叉树 | ||
// https://leetcode-cn.com/problems/invert-binary-tree/ | ||
|
||
class Solution226 { | ||
func invertTree(_ root: TreeNode?) -> TreeNode? { | ||
guard let root = root else { return nil } | ||
let left = invertTree(root.left) | ||
let right = invertTree(root.right) | ||
|
||
root.left = right | ||
root.right = left | ||
return root | ||
} | ||
} |
This file contains 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
This file contains 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,38 @@ | ||
// | ||
// Solution254.swift | ||
// LeetCodeForMac | ||
// | ||
// Created by 晋先森 on 2020/5/17. | ||
// Copyright © 2020 晋先森. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// 254. 因子的组合 | ||
// https://leetcode-cn.com/problems/factor-combinations/ | ||
|
||
class Solution254 { | ||
|
||
#warning("254") | ||
// func getFactors(_ n: Int) -> [[Int]] { | ||
// | ||
// guard n > 1 && n % 2 == 0 else { | ||
// return [] | ||
// } | ||
// | ||
// var results = [[Int]]() | ||
// | ||
// var i = 2 | ||
// while i * i < n { | ||
// | ||
// var temps = [Int]() | ||
// if n % i == 0 { | ||
// temps.append(<#T##newElement: Int##Int#>) | ||
// } | ||
// | ||
// i += 1 | ||
// } | ||
// | ||
// | ||
// } | ||
} |
Oops, something went wrong.