Skip to content

week01_29 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Week_01/id_29/leetcode_101_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// leetcode_101_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation

class Solution {

/*
思路:使用递归来实现 对称二叉树的规则是 左子树的左子树 等于 右子树的右子树 ,左子树的右子树等于 右子树的左子树
*/

func isMirror(_ left:TreeNode? ,_ right:TreeNode?) -> Bool{
if left == nil && right != nil || left != nil && right == nil{
return false
}
if left == nil && right == nil {
return true
}

return left!.val == right!.val &&
isMirror(left?.left, right?.right) &&
isMirror(left?.right, right?.left)
}


func isSymmetric(_ root: TreeNode?) -> Bool {
return isMirror(root?.left,root?.right)
}
}
35 changes: 35 additions & 0 deletions Week_01/id_29/leetcode_1021_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// leetcode_1021_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation

class Solution {

/*
实现思路:要删除最外层的括号 所以第一个“(”不需要添加到新创建的字符串里面 保持l括号正确的情况下最后一个“)”不需要添加到新创建的字符串
*/

func removeOuterParentheses(_ S: String) -> String {
var res:String = String()
var left = 0
for c in S {
if c == "(" {
left += 1
if left > 1 {
res.append(c)
}
}else if c == ")" {
left -= 1
if left > 0 {
res.append(c)
}
}
}
return res
}
}
28 changes: 28 additions & 0 deletions Week_01/id_29/leetcode_1047_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// leetcode_1047_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation
class Solution {

/*
实现思路:使用栈的方式,入栈的时候跟栈顶元素进行比较 如果栈顶元素等于当前元素 则弹出栈顶元素
否则入栈
*/

func removeDuplicates(_ S: String) -> String {
var res:String = String()
for c in S {
if res.isEmpty || res.last! != c{
res.append(c)
}else {
res.popLast()
}
}
return res
}
}
24 changes: 24 additions & 0 deletions Week_01/id_29/leetcode_104_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// leetcode_104_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation

class Solution {

/*
思路:使用递归来实现 最大深度为左子树的最大深度 或者右子树的最大深度 中的最大值 + 1
*/

func maxDepth(_ root: TreeNode?) -> Int {
if root == nil {
return 0
}else{
return max(maxDepth(root?.left), maxDepth(root?.right)) + 1
}
}
}
35 changes: 35 additions & 0 deletions Week_01/id_29/leetcode_111_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// leetcode_111_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation

class Solution {

/*
使用递归:
这里重写了 min 方法
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。在[1, 2]对应的树中,很明显,结点1不是叶子结点,结点2才是。所以比较的时候 如何左边是0 返回右边的最小值 ,右边是0 返回左边的最小值 左边小于右边返回左边 ,否则就是右边
*/

func min(_ a: Int, _ b: Int) -> Int {
if a == 0 {
return b
} else if b == 0 {
return a
}
return a < b ? a : b
}

func minDepth(_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }

//min(minDepth(root?.left), minDepth(root?.right))

return min(minDepth_1(root.left), minDepth_1(root.right)) + 1
}
}
132 changes: 132 additions & 0 deletions Week_01/id_29/leetcode_15_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// leetcode_15_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation

class Solution {
/*
方法一 暴力法 时间复杂度O(n^3)
*/
func threeSum(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
for i in 0..<nums.count-2 {
for j in i+1..<nums.count-1 {
for m in j+1..<nums.count {
if nums[i] + nums[j] + nums[m] == 0 {
let num = [nums[i],nums[j],nums[m]].sorted()
if !result.contains(num){
result.append(num)
}
}
}
}
}
return result
}
}


class Solution_1 {
/*
使用两数之和来做 ,三数之和为0。那么遍历这个数组 看存不存在两数之和为这个数的相反数 时间复杂度0(n^2)*o(3*log(3)) 空间复杂度0(n)
*/
func threeSum(_ nums: [Int]) -> [[Int]] {
if nums.count < 3 {
return [[Int]]()
}

var map:[Int:Int] = [Int:Int]()

for i in 0..<nums.count {
if map[nums[i]] == nil {
map[nums[i]] = 1
}else {
let num = map[nums[i]]! + 1
map[nums[i]] = num
}
}

var result:[[Int]] = [[Int]]()

for i in 0..<nums.count-2 {
for j in i+1..<nums.count-1 {
if map[-(nums[i] + nums[j])] != nil {
var count = map[-(nums[i] + nums[j])]! // 这个数出现的次数

if nums[i] == -(nums[i] + nums[j]) {
count -= 1
}
if nums[j] == -(nums[i] + nums[j]) {
count -= 1
}
if count > 0 {
let num = [nums[i],nums[j],-(nums[i] + nums[j])].sorted()
if !result.contains(num) {
result.append(num)
}
}
}
}
}
return result
}
}


class Solution_2 {

/*
方法三 : 先排序 然后遍历 数组 ,遍历的时候才去两个指针一个从当前数的下一个开始 另一个从数组结尾开始 ,判断这两个数的和是否等于当前的数

*/

func threeSum(_ nums: [Int]) -> [[Int]] {
if nums.count < 3 {
return [[Int]]()
}
var newNums = nums.sorted()

var result:[[Int]] = [[Int]]()

for i in 0..<newNums.count - 2 {
if i > 0 && newNums[i] == newNums[i-1] {
continue
}

var left = i + 1
var right = newNums.count - 1
let need = 0 - newNums[i]

while left < right {
if newNums[left] + newNums[right] == need {

let num = [newNums[i],newNums[left],newNums[right]]

result.append(num)


while left < right && newNums[left] == newNums[left + 1] {
left += 1
}
while left < right && newNums[right] == newNums[right - 1] {
right -= 1
}

left += 1
right -= 1

}else if newNums[left] + newNums[right] >= need {
right -= 1
}else {
left += 1
}
}
}
return result
}
}
46 changes: 46 additions & 0 deletions Week_01/id_29/leetcode_189_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// leetcode_189_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation


/*
采用取反的方式
*/
class Solution {

func rotate(_ nums: inout [Int], _ k: Int) {
var rotateCount = k
if k > nums.count {
rotateCount = k % nums.count
}
nums[0..<rotateCount].reverse()
nums[rotateCount..<nums.count].reverse()
nums.reverse()
}
}

class Solution1 {

/*每挪动一个元素 ,遍历一遍 时间复杂度是o(n) * o(k)*/
func rotate(_ nums: inout [Int], _ k: Int) {
var rotateCount = k
if k > nums.count {
rotateCount = k % nums.count
}
while rotateCount > 0 {
let lastNum = nums.last!
for i in 0..<nums.count-1 {
nums[nums.count - 1 - i] = nums[nums.count - 2 - i]
}
nums[0] = lastNum
rotateCount -= 1
}
}
}

44 changes: 44 additions & 0 deletions Week_01/id_29/leetcode_21_29.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// leetcode_21_29.swift
// 极客时间
//
// Created by gavin hu on 2019/6/23.
// Copyright © 2019 gavin hu. All rights reserved.
//

import Foundation


public class ListNode {
public var val: Int
public var next: ListNode?
public init(_ val: Int) {
self.val = val
self.next = nil
}
}

class Solution {

/*
采用递归来合并两个有序链表 因为两个子链表是有序的 所以合并的时候可以采用递归的方式
# 递归终止条件: 其中一个节点为空, 此时返回另一个节点. 就算是空也没关系.
# 处理逻辑: 判断两个节点值的大小, 就让较小的那个的 next 指向下一个待确定的节点
# 递归下沉: 将较小节点的下一个结点, 和较大结点作为入参传入本函数
# 返回值 : 较小节点
*/

func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
if l1 == nil || l2 == nil {
return l1 == nil ? l2 : l1
}

if l1!.val < l2!.val {
l1?.next = mergeTwoLists(l1?.next, l2)
return l1
}else{
l2?.next = mergeTwoLists(l1, l2?.next)
return l2
}
}
}
Loading