Skip to content

Commit

Permalink
完成 60. Permutation Sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
aelam committed May 27, 2017
1 parent 0ff38a8 commit 02e910a
Show file tree
Hide file tree
Showing 6 changed files with 697 additions and 4 deletions.
65 changes: 65 additions & 0 deletions Algorithms/100. Same Tree/main.swift
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()










125 changes: 125 additions & 0 deletions Algorithms/199. Binary Tree Right Side View/main.swift
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))
35 changes: 35 additions & 0 deletions Algorithms/41. First Missing Positive/main.swift
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 {



}


}
}

49 changes: 45 additions & 4 deletions Algorithms/45. Jump Game II/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@

*/

class Solution2 {
func jump(_ nums: [Int]) -> Int {
let count = nums.count

if count <= 1 {
return 0
}

var steps = [Int](repeating: Int.min, count: count)
var stepCount = 0
steps[stepCount] = nums[0]
if steps[stepCount] == 0 {
return 0
}

if steps[stepCount] >= count - 1 {
return stepCount + 1
}
for i in 1..<count {
if steps[stepCount] >= i {
steps[stepCount+1] = max(steps[stepCount+1], steps[stepCount] + nums[i])

if steps[stepCount] >= count - 1 {
return stepCount
}
} else {
steps[stepCount+1] = max(steps[stepCount+1], steps[stepCount] + nums[i])
if steps[stepCount+1] >= count - 1 {
return stepCount+2
}
stepCount += 1
}
}

return 1

}

}


class Solution {
func jump(_ nums: [Int]) -> Int {
Expand Down Expand Up @@ -65,13 +105,14 @@ class Solution {



let solution = Solution()
let solution = Solution2()

// 0 5 10 15 20 25 30 35 38
//var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8, 1,5,9,6,5, 2,7,9,7,9, 6,9,4,1,6, 8,8,4,4,2, 0,3,8,5]
var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8, 1,5,9,6,5, 2,7,9,7,9, 6,9,4,1,6, 8,8,4,4,2,3,8,5]
var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8, 1,5,9,6,5, 2,7,9,7,9, 6,9,4,1,6, 8,8,4,4,2, 0,3,8,5]
//var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8, 1,5,9,6,5, 2,7,9,7,9, 6,9,4,1,6, 8,8,4,4,2,3,8,5]
//var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8]
print(solution.jump(data))
print(solution.jump([1,2]) == 1)
print(solution.jump([1,2,3]) == 2)
//print(data.count)
//print(solution.sol(&data, 38) == 0)
//print(solution.sol(&data, 36) == 1)
Expand Down
87 changes: 87 additions & 0 deletions Algorithms/60. Permutation Sequence/main.swift
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))
Loading

0 comments on commit 02e910a

Please sign in to comment.