Skip to content

feat: add swift implementation to lcci problem: No.03.05 #2643

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
Apr 23, 2024
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();
*/
Loading