-
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
Showing
6 changed files
with
697 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// main.swift | ||
// 100. Same Tree | ||
// | ||
// Created by ryan on 26/05/2017. | ||
// Copyright © 2017 ryan. All rights reserved. | ||
// | ||
|
||
/* | ||
Given two binary trees, write a function to check if they are equal or not. | ||
|
||
Two binary trees are considered equal if they are structurally identical and the nodes have the same value. | ||
|
||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* public var val: Int | ||
* public var left: TreeNode? | ||
* public var right: TreeNode? | ||
* public init(_ val: Int) { | ||
* self.val = val | ||
* self.left = nil | ||
* self.right = nil | ||
* } | ||
* } | ||
*/ | ||
|
||
public class TreeNode { | ||
public var val: Int | ||
public var left: TreeNode? | ||
public var right: TreeNode? | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.left = nil | ||
self.right = nil | ||
} | ||
} | ||
|
||
class Solution { | ||
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { | ||
if p == nil && q == nil { | ||
return true | ||
} else if p != nil && q != nil && p!.val == q!.val { | ||
return (isSameTree(p?.left, q?.left) && | ||
isSameTree(p?.right, q?.right)) | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
|
||
let solution = Solution() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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,125 @@ | ||
// | ||
// main.swift | ||
// 199. Binary Tree Right Side View | ||
// | ||
// Created by ryan on 26/05/2017. | ||
// Copyright © 2017 ryan. All rights reserved. | ||
// | ||
|
||
/* | ||
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. | ||
|
||
For example: | ||
Given the following binary tree, | ||
1 <--- | ||
/ \ | ||
2 3 <--- | ||
\ \ | ||
5 4 <--- | ||
|
||
You should return [1, 3, 4]. | ||
*/ | ||
|
||
|
||
/** | ||
Definition for a binary tree node. | ||
public class TreeNode { | ||
public var val: Int | ||
public var left: TreeNode? | ||
public var right: TreeNode? | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.left = nil | ||
self.right = nil | ||
} | ||
} | ||
*/ | ||
|
||
// 按层遍历 | ||
|
||
public class TreeNode { | ||
public var val: Int | ||
public var left: TreeNode? | ||
public var right: TreeNode? | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.left = nil | ||
self.right = nil | ||
} | ||
|
||
} | ||
|
||
class SimpleSolution { | ||
func rightSideView(_ root: TreeNode?) -> [Int] { | ||
var results = [Int]() | ||
|
||
right(root, depth: 0, results: &results) | ||
|
||
return results | ||
} | ||
|
||
|
||
func right(_ node: TreeNode?, depth: Int, results: inout [Int]) { | ||
guard let node = node else { | ||
return | ||
} | ||
|
||
if results.count == depth { | ||
results.append(node.val) | ||
} | ||
|
||
right(node.right, depth: depth + 1, results: &results) | ||
right(node.left, depth: depth + 1, results: &results) | ||
|
||
} | ||
} | ||
|
||
|
||
class Solution { | ||
func rightSideView(_ root: TreeNode?) -> [Int] { | ||
var visibleValues = [Int]() | ||
let p = root | ||
var queue = [[TreeNode]]() | ||
|
||
if p != nil { | ||
queue.append([p!]) | ||
} else { | ||
return [] | ||
} | ||
|
||
while queue.count > 0 { | ||
let node = queue.remove(at: 0) | ||
if let first = node.first { | ||
visibleValues.append(first.val) | ||
} | ||
var children = [TreeNode]() | ||
for t in node { | ||
if t.right != nil { | ||
children.append(t.right!) | ||
} | ||
if t.left != nil { | ||
children.append(t.left!) | ||
} | ||
} | ||
if children.count > 0 { | ||
queue.append(children) | ||
} | ||
} | ||
|
||
return visibleValues | ||
} | ||
} | ||
|
||
|
||
let solution = SimpleSolution() | ||
let root = TreeNode(1) | ||
let p2 = TreeNode(2) | ||
let p3 = TreeNode(3) | ||
let p4 = TreeNode(4) | ||
|
||
|
||
root.left = p2 | ||
root.right = p3 | ||
p2.left = p4 | ||
|
||
print(solution.rightSideView(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// main.swift | ||
// 41. First Missing Positive | ||
// | ||
// Created by ryan on 25/05/2017. | ||
// Copyright © 2017 ryan. All rights reserved. | ||
// | ||
|
||
/* | ||
Given an unsorted integer array, find the first missing positive integer. | ||
|
||
For example, | ||
Given [1,2,0] return 3, | ||
and [3,4,-1,1] return 2. | ||
|
||
Your algorithm should run in O(n) time and uses constant space. | ||
|
||
*/ | ||
|
||
|
||
class Solution { | ||
func firstMissingPositive(_ nums: [Int]) -> Int { | ||
var min = nums.first - 1 | ||
var max = nums.first + 1 | ||
|
||
for i in 1..<nums.count { | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} | ||
} | ||
|
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,87 @@ | ||
// | ||
// main.swift | ||
// 60. Permutation Sequence | ||
// | ||
// Created by ryan on 27/05/2017. | ||
// Copyright © 2017 ryan. All rights reserved. | ||
// | ||
|
||
/* | ||
|
||
The set [1,2,3,…,n] contains a total of n! unique permutations. | ||
|
||
By listing and labeling all of the permutations in order, | ||
We get the following sequence (ie, for n = 3): | ||
|
||
"123" | ||
"132" | ||
"213" | ||
"231" | ||
"312" | ||
"321" | ||
Given n and k, return the kth permutation sequence. | ||
|
||
Note: Given n will be between 1 and 9 inclusive. | ||
|
||
*/ | ||
|
||
|
||
class Solution { | ||
func getPermutation(_ n: Int, _ k: Int) -> String { | ||
if n == 1 { | ||
return "1" | ||
} | ||
|
||
var intArray = (1...n).map { $0 } | ||
let permuCount = A(n - 1, 1) | ||
return getPermutation(&intArray, permuCount, k - 1) | ||
} | ||
|
||
func getPermutation(_ input: inout [Int], _ permuCount: Int , _ k: Int) -> String { | ||
if input.count <= 1 { | ||
return String(input.first!) | ||
} | ||
|
||
var results = String() | ||
let outputIndex = k / permuCount | ||
let output = input.remove(at: outputIndex) | ||
results += String(output) | ||
|
||
let newPermuCount = permuCount / input.count | ||
results += getPermutation(&input, newPermuCount, k % permuCount) | ||
|
||
return results | ||
|
||
} | ||
|
||
func getbang(_ n: Int) -> Int { | ||
let f = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] | ||
return f[n] | ||
} | ||
|
||
func A(_ n: Int, _ m: Int) -> Int { | ||
if m == 1 { | ||
return getbang(n) | ||
} | ||
|
||
var result = 1 | ||
if n == m { | ||
return n | ||
} | ||
for i in m...n { | ||
result *= i | ||
} | ||
return result | ||
} | ||
} | ||
|
||
|
||
|
||
let solution = Solution() | ||
//print(solution.A(4, 1)) | ||
print(solution.getPermutation(2, 2)) | ||
print(solution.getPermutation(4, 1)) | ||
print(solution.getPermutation(4, 2)) | ||
print(solution.getPermutation(4, 3)) | ||
print(solution.getPermutation(4, 4)) | ||
print(solution.getPermutation(4, 5)) |
Oops, something went wrong.