Skip to content

Commit 02e910a

Browse files
committed
完成 60. Permutation Sequence
1 parent 0ff38a8 commit 02e910a

File tree

6 files changed

+697
-4
lines changed

6 files changed

+697
-4
lines changed

Algorithms/100. Same Tree/main.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// main.swift
3+
// 100. Same Tree
4+
//
5+
// Created by ryan on 26/05/2017.
6+
// Copyright © 2017 ryan. All rights reserved.
7+
//
8+
9+
/*
10+
Given two binary trees, write a function to check if they are equal or not.
11+
12+
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
13+
14+
*/
15+
16+
/**
17+
* Definition for a binary tree node.
18+
* public class TreeNode {
19+
* public var val: Int
20+
* public var left: TreeNode?
21+
* public var right: TreeNode?
22+
* public init(_ val: Int) {
23+
* self.val = val
24+
* self.left = nil
25+
* self.right = nil
26+
* }
27+
* }
28+
*/
29+
30+
public class TreeNode {
31+
public var val: Int
32+
public var left: TreeNode?
33+
public var right: TreeNode?
34+
public init(_ val: Int) {
35+
self.val = val
36+
self.left = nil
37+
self.right = nil
38+
}
39+
}
40+
41+
class Solution {
42+
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
43+
if p == nil && q == nil {
44+
return true
45+
} else if p != nil && q != nil && p!.val == q!.val {
46+
return (isSameTree(p?.left, q?.left) &&
47+
isSameTree(p?.right, q?.right))
48+
} else {
49+
return false
50+
}
51+
}
52+
}
53+
54+
55+
let solution = Solution()
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// main.swift
3+
// 199. Binary Tree Right Side View
4+
//
5+
// Created by ryan on 26/05/2017.
6+
// Copyright © 2017 ryan. All rights reserved.
7+
//
8+
9+
/*
10+
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.
11+
12+
For example:
13+
Given the following binary tree,
14+
1 <---
15+
/ \
16+
2 3 <---
17+
\ \
18+
5 4 <---
19+
20+
You should return [1, 3, 4].
21+
*/
22+
23+
24+
/**
25+
Definition for a binary tree node.
26+
public class TreeNode {
27+
public var val: Int
28+
public var left: TreeNode?
29+
public var right: TreeNode?
30+
public init(_ val: Int) {
31+
self.val = val
32+
self.left = nil
33+
self.right = nil
34+
}
35+
}
36+
*/
37+
38+
// 按层遍历
39+
40+
public class TreeNode {
41+
public var val: Int
42+
public var left: TreeNode?
43+
public var right: TreeNode?
44+
public init(_ val: Int) {
45+
self.val = val
46+
self.left = nil
47+
self.right = nil
48+
}
49+
50+
}
51+
52+
class SimpleSolution {
53+
func rightSideView(_ root: TreeNode?) -> [Int] {
54+
var results = [Int]()
55+
56+
right(root, depth: 0, results: &results)
57+
58+
return results
59+
}
60+
61+
62+
func right(_ node: TreeNode?, depth: Int, results: inout [Int]) {
63+
guard let node = node else {
64+
return
65+
}
66+
67+
if results.count == depth {
68+
results.append(node.val)
69+
}
70+
71+
right(node.right, depth: depth + 1, results: &results)
72+
right(node.left, depth: depth + 1, results: &results)
73+
74+
}
75+
}
76+
77+
78+
class Solution {
79+
func rightSideView(_ root: TreeNode?) -> [Int] {
80+
var visibleValues = [Int]()
81+
let p = root
82+
var queue = [[TreeNode]]()
83+
84+
if p != nil {
85+
queue.append([p!])
86+
} else {
87+
return []
88+
}
89+
90+
while queue.count > 0 {
91+
let node = queue.remove(at: 0)
92+
if let first = node.first {
93+
visibleValues.append(first.val)
94+
}
95+
var children = [TreeNode]()
96+
for t in node {
97+
if t.right != nil {
98+
children.append(t.right!)
99+
}
100+
if t.left != nil {
101+
children.append(t.left!)
102+
}
103+
}
104+
if children.count > 0 {
105+
queue.append(children)
106+
}
107+
}
108+
109+
return visibleValues
110+
}
111+
}
112+
113+
114+
let solution = SimpleSolution()
115+
let root = TreeNode(1)
116+
let p2 = TreeNode(2)
117+
let p3 = TreeNode(3)
118+
let p4 = TreeNode(4)
119+
120+
121+
root.left = p2
122+
root.right = p3
123+
p2.left = p4
124+
125+
print(solution.rightSideView(root))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// main.swift
3+
// 41. First Missing Positive
4+
//
5+
// Created by ryan on 25/05/2017.
6+
// Copyright © 2017 ryan. All rights reserved.
7+
//
8+
9+
/*
10+
Given an unsorted integer array, find the first missing positive integer.
11+
12+
For example,
13+
Given [1,2,0] return 3,
14+
and [3,4,-1,1] return 2.
15+
16+
Your algorithm should run in O(n) time and uses constant space.
17+
18+
*/
19+
20+
21+
class Solution {
22+
func firstMissingPositive(_ nums: [Int]) -> Int {
23+
var min = nums.first - 1
24+
var max = nums.first + 1
25+
26+
for i in 1..<nums.count {
27+
28+
29+
30+
}
31+
32+
33+
}
34+
}
35+

Algorithms/45. Jump Game II/main.swift

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@
2424

2525
*/
2626

27+
class Solution2 {
28+
func jump(_ nums: [Int]) -> Int {
29+
let count = nums.count
30+
31+
if count <= 1 {
32+
return 0
33+
}
34+
35+
var steps = [Int](repeating: Int.min, count: count)
36+
var stepCount = 0
37+
steps[stepCount] = nums[0]
38+
if steps[stepCount] == 0 {
39+
return 0
40+
}
41+
42+
if steps[stepCount] >= count - 1 {
43+
return stepCount + 1
44+
}
45+
for i in 1..<count {
46+
if steps[stepCount] >= i {
47+
steps[stepCount+1] = max(steps[stepCount+1], steps[stepCount] + nums[i])
48+
49+
if steps[stepCount] >= count - 1 {
50+
return stepCount
51+
}
52+
} else {
53+
steps[stepCount+1] = max(steps[stepCount+1], steps[stepCount] + nums[i])
54+
if steps[stepCount+1] >= count - 1 {
55+
return stepCount+2
56+
}
57+
stepCount += 1
58+
}
59+
}
60+
61+
return 1
62+
63+
}
64+
65+
}
66+
2767

2868
class Solution {
2969
func jump(_ nums: [Int]) -> Int {
@@ -65,13 +105,14 @@ class Solution {
65105

66106

67107

68-
let solution = Solution()
108+
let solution = Solution2()
69109

70110
// 0 5 10 15 20 25 30 35 38
71-
//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]
72-
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]
111+
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]
112+
//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]
73113
//var data = [5,6,4,4,6, 9,4,4,7,4, 4,8,2,6,8]
74-
print(solution.jump(data))
114+
print(solution.jump([1,2]) == 1)
115+
print(solution.jump([1,2,3]) == 2)
75116
//print(data.count)
76117
//print(solution.sol(&data, 38) == 0)
77118
//print(solution.sol(&data, 36) == 1)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// main.swift
3+
// 60. Permutation Sequence
4+
//
5+
// Created by ryan on 27/05/2017.
6+
// Copyright © 2017 ryan. All rights reserved.
7+
//
8+
9+
/*
10+
11+
The set [1,2,3,…,n] contains a total of n! unique permutations.
12+
13+
By listing and labeling all of the permutations in order,
14+
We get the following sequence (ie, for n = 3):
15+
16+
"123"
17+
"132"
18+
"213"
19+
"231"
20+
"312"
21+
"321"
22+
Given n and k, return the kth permutation sequence.
23+
24+
Note: Given n will be between 1 and 9 inclusive.
25+
26+
*/
27+
28+
29+
class Solution {
30+
func getPermutation(_ n: Int, _ k: Int) -> String {
31+
if n == 1 {
32+
return "1"
33+
}
34+
35+
var intArray = (1...n).map { $0 }
36+
let permuCount = A(n - 1, 1)
37+
return getPermutation(&intArray, permuCount, k - 1)
38+
}
39+
40+
func getPermutation(_ input: inout [Int], _ permuCount: Int , _ k: Int) -> String {
41+
if input.count <= 1 {
42+
return String(input.first!)
43+
}
44+
45+
var results = String()
46+
let outputIndex = k / permuCount
47+
let output = input.remove(at: outputIndex)
48+
results += String(output)
49+
50+
let newPermuCount = permuCount / input.count
51+
results += getPermutation(&input, newPermuCount, k % permuCount)
52+
53+
return results
54+
55+
}
56+
57+
func getbang(_ n: Int) -> Int {
58+
let f = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
59+
return f[n]
60+
}
61+
62+
func A(_ n: Int, _ m: Int) -> Int {
63+
if m == 1 {
64+
return getbang(n)
65+
}
66+
67+
var result = 1
68+
if n == m {
69+
return n
70+
}
71+
for i in m...n {
72+
result *= i
73+
}
74+
return result
75+
}
76+
}
77+
78+
79+
80+
let solution = Solution()
81+
//print(solution.A(4, 1))
82+
print(solution.getPermutation(2, 2))
83+
print(solution.getPermutation(4, 1))
84+
print(solution.getPermutation(4, 2))
85+
print(solution.getPermutation(4, 3))
86+
print(solution.getPermutation(4, 4))
87+
print(solution.getPermutation(4, 5))

0 commit comments

Comments
 (0)