Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33b4b5a

Browse files
feat: add swift implementation to lcci problem: No.03.05 (doocs#2643)
1 parent 1396613 commit 33b4b5a

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

‎lcci/03.05.Sort of Stacks/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,49 @@ impl SortedStack {
314314
*/
315315
```
316316

317+
```swift
318+
class SortedStack {
319+
private var stk: [Int] = []
320+
321+
init() {}
322+
323+
func push(_ val: Int) {
324+
var temp: [Int] = []
325+
while let top = stk.last, top < val {
326+
temp.append(stk.removeLast())
327+
}
328+
stk.append(val)
329+
while let last = temp.popLast() {
330+
stk.append(last)
331+
}
332+
}
333+
334+
func pop() {
335+
if !isEmpty() {
336+
stk.removeLast()
337+
}
338+
}
339+
340+
func peek() -> Int {
341+
return isEmpty() ? -1 : stk.last!
342+
}
343+
344+
func isEmpty() -> Bool {
345+
return stk.isEmpty
346+
}
347+
}
348+
349+
/**
350+
* Your SortedStack object will be instantiated and called as such:
351+
* let obj = new SortedStack();
352+
* obj.push(val);
353+
* obj.pop();
354+
* let param_3 = obj.peek();
355+
* var myVar: Bool;
356+
* myVar = obj.isEmpty();
357+
*/
358+
```
359+
317360
<!-- tabs:end -->
318361

319362
<!-- end -->

‎lcci/03.05.Sort of Stacks/README_EN.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,49 @@ impl SortedStack {
327327
*/
328328
```
329329

330+
```swift
331+
class SortedStack {
332+
private var stk: [Int] = []
333+
334+
init() {}
335+
336+
func push(_ val: Int) {
337+
var temp: [Int] = []
338+
while let top = stk.last, top < val {
339+
temp.append(stk.removeLast())
340+
}
341+
stk.append(val)
342+
while let last = temp.popLast() {
343+
stk.append(last)
344+
}
345+
}
346+
347+
func pop() {
348+
if !isEmpty() {
349+
stk.removeLast()
350+
}
351+
}
352+
353+
func peek() -> Int {
354+
return isEmpty() ? -1 : stk.last!
355+
}
356+
357+
func isEmpty() -> Bool {
358+
return stk.isEmpty
359+
}
360+
}
361+
362+
/**
363+
* Your SortedStack object will be instantiated and called as such:
364+
* let obj = new SortedStack();
365+
* obj.push(val);
366+
* obj.pop();
367+
* let param_3 = obj.peek();
368+
* var myVar: Bool;
369+
* myVar = obj.isEmpty();
370+
*/
371+
```
372+
330373
<!-- tabs:end -->
331374

332375
<!-- end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class SortedStack {
2+
private var stk: [Int] = []
3+
4+
init() {}
5+
6+
func push(_ val: Int) {
7+
var temp: [Int] = []
8+
while let top = stk.last, top < val {
9+
temp.append(stk.removeLast())
10+
}
11+
stk.append(val)
12+
while let last = temp.popLast() {
13+
stk.append(last)
14+
}
15+
}
16+
17+
func pop() {
18+
if !isEmpty() {
19+
stk.removeLast()
20+
}
21+
}
22+
23+
func peek() -> Int {
24+
return isEmpty() ? -1 : stk.last!
25+
}
26+
27+
func isEmpty() -> Bool {
28+
return stk.isEmpty
29+
}
30+
}
31+
32+
/**
33+
* Your SortedStack object will be instantiated and called as such:
34+
* let obj = new SortedStack();
35+
* obj.push(val);
36+
* obj.pop();
37+
* let param_3 = obj.peek();
38+
* var myVar: Bool;
39+
* myVar = obj.isEmpty();
40+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /