Skip to content
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
43 changes: 43 additions & 0 deletions lcci/03.05.Sort of Stacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,49 @@ impl SortedStack {
*/
```

```swift
class SortedStack {
private var stk: [Int] = []

init() {}

func push(_ val: Int) {
var temp: [Int] = []
while let top = stk.last, top < val {
temp.append(stk.removeLast())
}
stk.append(val)
while let last = temp.popLast() {
stk.append(last)
}
}

func pop() {
if !isEmpty() {
stk.removeLast()
}
}

func peek() -> Int {
return isEmpty() ? -1 : stk.last!
}

func isEmpty() -> Bool {
return stk.isEmpty
}
}

/**
* Your SortedStack object will be instantiated and called as such:
* let obj = new SortedStack();
* obj.push(val);
* obj.pop();
* let param_3 = obj.peek();
* var myVar: Bool;
* myVar = obj.isEmpty();
*/
```

<!-- tabs:end -->

<!-- end -->
43 changes: 43 additions & 0 deletions lcci/03.05.Sort of Stacks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,49 @@ impl SortedStack {
*/
```

```swift
class SortedStack {
private var stk: [Int] = []

init() {}

func push(_ val: Int) {
var temp: [Int] = []
while let top = stk.last, top < val {
temp.append(stk.removeLast())
}
stk.append(val)
while let last = temp.popLast() {
stk.append(last)
}
}

func pop() {
if !isEmpty() {
stk.removeLast()
}
}

func peek() -> Int {
return isEmpty() ? -1 : stk.last!
}

func isEmpty() -> Bool {
return stk.isEmpty
}
}

/**
* Your SortedStack object will be instantiated and called as such:
* let obj = new SortedStack();
* obj.push(val);
* obj.pop();
* let param_3 = obj.peek();
* var myVar: Bool;
* myVar = obj.isEmpty();
*/
```

<!-- tabs:end -->

<!-- end -->
40 changes: 40 additions & 0 deletions lcci/03.05.Sort of Stacks/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class SortedStack {
private var stk: [Int] = []

init() {}

func push(_ val: Int) {
var temp: [Int] = []
while let top = stk.last, top < val {
temp.append(stk.removeLast())
}
stk.append(val)
while let last = temp.popLast() {
stk.append(last)
}
}

func pop() {
if !isEmpty() {
stk.removeLast()
}
}

func peek() -> Int {
return isEmpty() ? -1 : stk.last!
}

func isEmpty() -> Bool {
return stk.isEmpty
}
}

/**
* Your SortedStack object will be instantiated and called as such:
* let obj = new SortedStack();
* obj.push(val);
* obj.pop();
* let param_3 = obj.peek();
* var myVar: Bool;
* myVar = obj.isEmpty();
*/