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 676e17d

Browse files
committed
add 8, 23, 689, 1014, 1422 ans update 22 at swift
1 parent daf8f2a commit 676e17d

6 files changed

+215
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 8. String to Integer (atoi)
3+
* https://leetcode.com/problems/string-to-integer-atoi/
4+
**/
5+
6+
import Foundation
7+
8+
class Solution {
9+
func myAtoi(_ s: String) -> Int {
10+
var ans = 0, chars = Array(s.trimmingCharacters(in: .whitespacesAndNewlines)).map { String(0ドル) }
11+
var isNumberStarted = false, isMinus = false
12+
13+
for ch in chars {
14+
if ch == "+" && !isNumberStarted {
15+
isNumberStarted = true
16+
} else if ch == "-" && !isNumberStarted {
17+
isMinus = true
18+
isNumberStarted = true
19+
} else if ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].contains(ch) {
20+
isNumberStarted = true
21+
ans = ans * 10 + (Int(ch)! * (isMinus ? -1 : 1))
22+
} else {
23+
break
24+
}
25+
26+
if ans > Int(Int32.max) { ans = Int(Int32.max); break}
27+
if ans < Int(Int32.min) { ans = Int(Int32.min); break}
28+
}
29+
30+
return ans
31+
}
32+
}

‎swift/0022-generate-parentheses.swift‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,27 @@ class Solution {
2626

2727
return result
2828
}
29+
}
30+
31+
class Solution2 {
32+
func generateParenthesis(_ n: Int) -> [String] {
33+
var stack: [(s: String, opened: Int)] = [("", 0)]
34+
35+
while stack[0].s.count < n * 2 {
36+
var newStack: [(s: String, opened: Int)] = []
37+
38+
for row in stack {
39+
if n * 2 - row.s.count - row.opened >= 2 {
40+
newStack.append( (row.s + "(", row.opened + 1) )
41+
}
42+
if row.opened > 0 {
43+
newStack.append( (row.s + ")", row.opened - 1) )
44+
}
45+
}
46+
47+
stack = newStack
48+
}
49+
50+
return stack.map { 0ドル.s }
51+
}
2952
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 23. Merge k Sorted Lists
3+
* https://leetcode.com/problems/merge-k-sorted-lists/
4+
**/
5+
6+
/**
7+
* Definition for singly-linked list.
8+
* public class ListNode {
9+
* public var val: Int
10+
* public var next: ListNode?
11+
* public init() { self.val = 0; self.next = nil; }
12+
* public init(_ val: Int) { self.val = val; self.next = nil; }
13+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
14+
* }
15+
*/
16+
class Solution {
17+
func mergeKLists(_ lists: [ListNode?]) -> ListNode? {
18+
let head: ListNode? = ListNode()
19+
var cur: ListNode? = head, lists = lists
20+
21+
while lists.count > 0 {
22+
while lists.count > 0 && lists[lists.count-1] == nil { lists.popLast() }
23+
if lists.isEmpty { break }
24+
25+
var nextIndex: Int?, i = 0
26+
while i < lists.count {
27+
if lists[i] != nil {
28+
if nextIndex == nil || lists[i]!.val < lists[nextIndex!]!.val {
29+
nextIndex = i
30+
}
31+
}
32+
i += 1
33+
}
34+
35+
cur!.next = lists[nextIndex!]
36+
cur = cur?.next
37+
lists[nextIndex!] = lists[nextIndex!]!.next
38+
}
39+
40+
return head!.next
41+
}
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 689. Maximum Sum of 3 Non-Overlapping Subarrays
3+
* https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/
4+
**/
5+
6+
class Solution {
7+
func maxSumOfThreeSubarrays(_ nums: [Int], _ k: Int) -> [Int] {
8+
var dp: [(sum: Int, ind: Int)] = []
9+
10+
var l = 0, r = -1, sum = 0
11+
while r < nums.count-1 {
12+
r += 1
13+
sum += nums[r]
14+
if r - l + 1 > k {
15+
sum -= nums[l]
16+
l += 1
17+
}
18+
if r - l + 1 == k {
19+
dp.append( (sum: sum, ind: l) )
20+
}
21+
}
22+
23+
var best_dp = [(left: Int?, right: Int?)](repeating: (nil, nil), count: dp.count)
24+
25+
var best = 0
26+
for i in 0 ... dp.count-1 {
27+
best_dp[i].left = dp[i].sum > dp[best].sum ? i : best
28+
best = best_dp[i].left!
29+
}
30+
31+
best = dp.count - 1
32+
for i in (0 ... dp.count-1).reversed() {
33+
best_dp[i].right = dp[i].sum >= dp[best].sum ? i : best
34+
best = best_dp[i].right!
35+
}
36+
37+
var ans: [Int] = [], ans_sum = 0
38+
39+
for i in k ... best_dp.count-k-1 {
40+
let s = dp[i].sum + dp[best_dp[i-k].left!].sum + dp[best_dp[i+k].right!].sum
41+
if s > ans_sum {
42+
ans_sum = s
43+
ans = [best_dp[i-k].left!, i, best_dp[i+k].right!]
44+
}
45+
}
46+
47+
return ans
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 1014. Best Sightseeing Pair
3+
* https://leetcode.com/problems/best-sightseeing-pair/
4+
**/
5+
6+
class Solution {
7+
func maxScoreSightseeingPair(_ values: [Int]) -> Int {
8+
var ans = values[0] + values[1] - 1
9+
10+
for i in 0 ..< values.count-1 {
11+
let isum = values[i] + i
12+
for j in i+1 ..< values.count {
13+
ans = max(ans, isum + values[j] - j)
14+
}
15+
}
16+
17+
return ans
18+
}
19+
}
20+
21+
class Solution2 {
22+
func maxScoreSightseeingPair(_ values: [Int]) -> Int {
23+
var ans = values[0] + values[1] - 1
24+
25+
var pref_sums: [Int] = [ values[0] ]
26+
27+
for i in 1 ..< values.count {
28+
pref_sums.append( max(pref_sums[i-1], values[i] + i) )
29+
}
30+
31+
var post_sums: [Int] = [ values[values.count-1] - values.count + 1 ]
32+
33+
for j in (0 ... values.count-2).reversed() {
34+
post_sums.append( max(post_sums[post_sums.count-1], values[j] - j) )
35+
}
36+
37+
post_sums = post_sums.reversed()
38+
39+
for i in 0 ..< values.count-1 {
40+
ans = max(ans, pref_sums[i] + post_sums[i+1])
41+
}
42+
43+
return ans
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 1422. Maximum Score After Splitting a String
3+
* https://leetcode.com/problems/maximum-score-after-splitting-a-string/
4+
**/
5+
6+
class Solution {
7+
func maxScore(_ s: String) -> Int {
8+
let chars = Array(s).map { String(0ドル) }
9+
var zeros = 0, ones = chars.filter { 0ドル == "1" }.count
10+
11+
var i = 0, ans = 0
12+
while i <= chars.count - 2 {
13+
if chars[i] == "0" {
14+
zeros += 1
15+
} else {
16+
ones -= 1
17+
}
18+
ans = max(ans, zeros + ones)
19+
i += 1
20+
}
21+
22+
return ans
23+
}
24+
}

0 commit comments

Comments
(0)

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