Skip to content

Commit 887f3dd

Browse files
authored
Merge pull request #1795 from delight010/main
[SeongA] WEEK 03 Solutions
2 parents 402655d + dfa0f77 commit 887f3dd

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

combination-sum/delight010.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
3+
var result: [[Int]] = []
4+
var current: [Int] = []
5+
findCombination(0, target, candidates, &current, &result)
6+
return result
7+
}
8+
9+
func findCombination(_ index: Int, _ target: Int, _ candidates: [Int], _ current: inout [Int], _ result: inout [[Int]]) {
10+
if target == 0 {
11+
result.append(current)
12+
return
13+
}
14+
15+
for i in index..<candidates.count {
16+
if candidates[i] <= target {
17+
current.append(candidates[i])
18+
findCombination(i, target - candidates[i], candidates, &current, &result)
19+
current.removeLast()
20+
}
21+
}
22+
}
23+
}
24+

decode-ways/delight010.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func numDecodings(_ s: String) -> Int {
3+
var array = Array(s)
4+
var dp: [Int: Int] = [array.count: 1]
5+
for i in stride(from: array.count - 1, to: -1, by: -1) {
6+
if array[i] == "0" {
7+
dp[i] = 0
8+
} else {
9+
dp[i] = dp[i + 1]
10+
}
11+
12+
if i + 1 < array.count && (array[i] == "1" || array[i] == "2" && "0123456".contains(array[i + 1])) {
13+
dp[i, default: 0] += dp[i + 2] ?? 0
14+
}
15+
}
16+
return dp[0]!
17+
}
18+
}
19+

maximum-subarray/delight010.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func maxSubArray(_ nums: [Int]) -> Int {
3+
var maxSum = nums[0]
4+
var maxEndingHere = nums[0]
5+
for i in 1..<nums.count {
6+
maxEndingHere = max(nums[i], maxEndingHere + nums[i])
7+
maxSum = max(maxSum, maxEndingHere)
8+
}
9+
return maxSum
10+
}
11+
}
12+

number-of-1-bits/delight010.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
func hammingWeight(_ n: Int) -> Int {
3+
var number = n
4+
var answer = 0
5+
while number > 0 {
6+
if number & 1 == 1 {
7+
answer += 1
8+
}
9+
number = number >> 1
10+
}
11+
return answer
12+
}
13+
}
14+

valid-palindrome/delight010.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
func isPalindrome(_ s: String) -> Bool {
3+
let string = s.replacingOccurrences(of: " ", with: "").lowercased().map { $0 }
4+
var leftIndex = string.startIndex
5+
var rightIndex = string.endIndex - 1
6+
let letterRange: ClosedRange<UInt8> = 97...122
7+
let numberRange: ClosedRange<UInt8> = 48...57
8+
while leftIndex <= rightIndex {
9+
guard let leftCharAscii = string[leftIndex].asciiValue else { break }
10+
guard let rightCharAscii = string[rightIndex].asciiValue else { break }
11+
if !letterRange.contains(leftCharAscii) && !numberRange.contains(leftCharAscii) {
12+
leftIndex += 1
13+
continue
14+
}
15+
if !letterRange.contains(rightCharAscii) && !numberRange.contains(rightCharAscii) {
16+
rightIndex -= 1
17+
continue
18+
}
19+
if leftCharAscii == rightCharAscii {
20+
leftIndex += 1
21+
rightIndex -= 1
22+
} else {
23+
return false
24+
}
25+
}
26+
return true
27+
}
28+
}
29+

0 commit comments

Comments
 (0)