Skip to content

Commit

Permalink
add ZigZag
Browse files Browse the repository at this point in the history
  • Loading branch information
aelam committed May 24, 2017
1 parent 4a94c25 commit fcacf91
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Algorithms/223. Rectangle Area/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// main.swift
// 223. Rectangle Area
//
// Created by ryan on 23/05/2017.
// Copyright © 2017 ryan. All rights reserved.
//

/*
Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.
*/


95 changes: 95 additions & 0 deletions Algorithms/23. Merge k Sorted Lists/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// main.swift
// 23. Merge k Sorted Lists
//
// Created by ryan on 23/05/2017.
// Copyright © 2017 ryan. All rights reserved.
//

/**
*
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
*
*/


/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/

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

//TODO: 解法超时, 需要优化
class Solution {
func mergeKLists(_ lists: [ListNode?]) -> ListNode? {
if lists.count == 0 {
return nil
} else if lists.count == 1 {
return lists.first!
}
var listNode: ListNode? = nil
for list in lists {
listNode = mergeList(listNode, list)
}

return listNode
}

func mergeList(_ listA: ListNode?, _ listB: ListNode?) -> ListNode? {
if listA == nil {
return listB
} else if listB == nil {
return listA
}

if listA!.val < listB!.val {
let right = mergeList(listA!.next, listB)
listA?.next = right
return listA
} else {
let right = mergeList(listB!.next, listA)
listB?.next = right
return listB
}
}
}

func createLists(_ values: [[Int]]) -> [ListNode?] {
var lists: [ListNode?] = [ListNode?]()

for value in values {
let list = createList(value)
lists.append(list)
}
return lists
}

func createList(_ values: [Int]) -> ListNode {
let first = ListNode(values[0])
var pivot = first
for i in 1..<values.count {
pivot.next = ListNode(values[i])
pivot = pivot.next!
}
return first
}


let soluition = Solution()
let lists = createLists([[7],[49],[73],[58],[30],[72],[44],[78],[23]])
soluition.mergeKLists(lists)
54 changes: 54 additions & 0 deletions Algorithms/6. ZigZag Conversion/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// main.swift
// 6. ZigZag Conversion
//
// Created by ryan on 24/05/2017.
// Copyright © 2017 ryan. All rights reserved.
//

/*
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/


// 常规方法按照Z型走势依次给字符串赋值
class Solution {
func convert(_ s: String, _ numRows: Int) -> String {
var results = [[Character]]()

var row = 0
var direction = 1
for c in s.characters {
if results.count <= row {
results.append([])
}
results[row].append(c)
if row == 0 {
direction = 1
} else if row == numRows - 1 {
direction = -1
}
row += direction
}

var result = ""
for r in results {
result += String(r)
}
return result
}
}


let solution = Solution()
print(solution.convert("PAYPALISHIRING", 3))
print(solution.convert("PAYPALISHIRING", 4) == "PINALSIGYAHRPI")
Loading

0 comments on commit fcacf91

Please sign in to comment.