Skip to content

Commit 3aaf574

Browse files
authored
Merge pull request algorithm004-01#371 from z-wade/master
396-Week 02
2 parents ec038e6 + b02d9a6 commit 3aaf574

4 files changed

Lines changed: 242 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// LeetCode_242_396.swift
3+
//
4+
//
5+
// Created by chenjunzhi on 2019/10/27.
6+
//
7+
8+
import UIKit
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init(_ val: Int) {
16+
* self.val = val
17+
* self.left = nil
18+
* self.right = nil
19+
* }
20+
* }
21+
*/
22+
23+
//二叉树前序遍历
24+
class Solution {
25+
func preorderTraversal(_ root: TreeNode?) -> [Int] {
26+
guard let tree = root else {
27+
return []
28+
}
29+
30+
var current:TreeNode!
31+
var result : [Int] = []
32+
var stack: [TreeNode] = [tree]
33+
while stack.count > 0 {
34+
//遍历当前节点
35+
current = stack.last
36+
result.append(current.val)
37+
stack.removeLast()
38+
//右节点进入栈底
39+
if let right = current.right {
40+
stack.append(right)
41+
}
42+
//再左节点进入栈
43+
if let left = current.left {
44+
stack.append(left)
45+
}
46+
}
47+
return result
48+
}
49+
50+
// func preorderTraversal(_ root: TreeNode?) -> [Int] {
51+
// guard let root = root else {
52+
// return []
53+
// }
54+
// let left = preorderTraversal(root.left)
55+
// let right = preorderTraversal(root.right)
56+
// return [root.val] + left + right
57+
// }
58+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// LeetCode_242_396.swift
3+
//
4+
//
5+
// Created by chenjunzhi on 2019/10/27.
6+
//
7+
8+
import UIKit
9+
10+
class Solution {
11+
12+
/// 思路:把每个字母变成Unicode编码。利用桶的思想,计算每个字符显示的次数。最后比较两个桶是否一样
13+
func isAnagram(_ s: String, _ t: String) -> Bool {
14+
guard s.count == t.count else { return false }
15+
16+
let aUnicode = UnicodeScalar("a").value
17+
18+
var counterS = Array(repeating: 0, count: 26)
19+
for c in s.unicodeScalars {
20+
counterS[Int(c.value - aUnicode)] += 1
21+
}
22+
23+
var counterT = Array(repeating: 0, count: 26)
24+
for c in t.unicodeScalars {
25+
counterT[Int(c.value - aUnicode)] += 1
26+
}
27+
28+
return counterS == counterT
29+
}
30+
31+
/// 把每个字母根据Ascii码进行桶排序,然后比较两个字符串是否相等
32+
class Solution1 {
33+
func isAnagram(_ s: String, _ t: String) -> Bool {
34+
let charS = sort(Array(s))
35+
let charT = sort(Array(t))
36+
return charT == charS
37+
}
38+
39+
func sort(_ chars: [Character]) -> String {
40+
let charVal = Character("a").asciiValue!
41+
var buckets = Array(repeating: 0, count: 26)
42+
for char in chars {
43+
let index = char.asciiValue! - charVal
44+
buckets[Int(index)] += 1
45+
}
46+
var result = [Character]()
47+
for index in 0..<buckets.count {
48+
let count = buckets[index]
49+
for _ in 0..<count {
50+
result.append(Character(UnicodeScalar(index + Int(charVal))!))
51+
}
52+
}
53+
return String(result)
54+
}
55+
}
56+
57+
class Solution2 {
58+
func isAnagram(_ s: String, _ t: String) -> Bool {
59+
var dicS = [Character: Int]()
60+
if s.count != t.count {
61+
return false
62+
}
63+
64+
for sChar in s {
65+
if let count = dicS[sChar] {
66+
dicS[sChar] = count + 1
67+
} else {
68+
dicS[sChar] = 1
69+
}
70+
}
71+
for tChar in t {
72+
if let count = dicS[tChar] {
73+
if count == 0 {
74+
return false;
75+
}
76+
dicS[tChar] = count - 1
77+
} else {
78+
return false;
79+
}
80+
}
81+
return true;
82+
}
83+
}
84+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public class NTreePreTraversal {
7+
8+
public List<Integer> preorder(Node root) {
9+
ArrayList<Integer> result = new ArrayList();
10+
LinkedList<Node> stack = new LinkedList();
11+
if (root == null){
12+
return result;
13+
}
14+
15+
stack.push(root);
16+
while (!stack.isEmpty()) {
17+
Node current = stack.pop();
18+
result.add(current.val);
19+
if (current.children != null) {
20+
for (int i = current.children.size() - 1; i >= 0; i--){
21+
stack.push(current.children.get(i));
22+
}
23+
}
24+
}
25+
26+
return result;
27+
}
28+
29+
public List<Integer> preorder2(Node root) {
30+
ArrayList<Integer> result = new ArrayList();
31+
LinkedList<Node> stack = new LinkedList();
32+
if (root == null){
33+
return result;
34+
}
35+
36+
result.add(root.val);
37+
for (int i = 0; i < root.children.size(); i++) {
38+
List<Integer> childResult = preorder(root.children.get(i));
39+
result.addAll(childResult);
40+
}
41+
return result;
42+
}
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// LeetCode_242_396.swift
3+
//
4+
//
5+
// Created by chenjunzhi on 2019/10/27.
6+
//
7+
8+
import UIKit
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* public class TreeNode {
13+
* public var val: Int
14+
* public var left: TreeNode?
15+
* public var right: TreeNode?
16+
* public init(_ val: Int) {
17+
* self.val = val
18+
* self.left = nil
19+
* self.right = nil
20+
* }
21+
* }
22+
*/
23+
24+
//基于栈的遍历
25+
class Solution {
26+
func inorderTraversal(_ root: TreeNode?) -> [Int] {
27+
var stack = [TreeNode]()
28+
var currentNode = root;
29+
var result = [Int]()
30+
31+
while currentNode != nil || stack.count != 0 {
32+
//左节点入栈
33+
while currentNode != nil {
34+
stack.append(currentNode!)
35+
currentNode = currentNode!.left
36+
}
37+
//出栈并把右节点入栈
38+
currentNode = stack.last
39+
stack.removeLast();
40+
result.append(currentNode!.val)
41+
currentNode = currentNode?.right
42+
}
43+
return result
44+
}
45+
}
46+
47+
//递归 时间复杂度O(n)
48+
class Solution {
49+
func inorderTraversal(_ root: TreeNode?) -> [Int] {
50+
guard let root = root else {
51+
return []
52+
}
53+
let left = inorderTraversal(root.left)
54+
let right = inorderTraversal(root.right)
55+
return left + [root.val] + right
56+
}
57+
}

0 commit comments

Comments
 (0)