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
24 changes: 24 additions & 0 deletions combination-sum/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
var result: [[Int]] = []
var current: [Int] = []
findCombination(0, target, candidates, &current, &result)
return result
}

func findCombination(_ index: Int, _ target: Int, _ candidates: [Int], _ current: inout [Int], _ result: inout [[Int]]) {
if target == 0 {
result.append(current)
return
}

for i in index..<candidates.count {
if candidates[i] <= target {
current.append(candidates[i])
findCombination(i, target - candidates[i], candidates, &current, &result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우와 백트래킹 방법으로 풀었다니 대단합니다..!! 👍👍

current.removeLast()
}
}
}
}

19 changes: 19 additions & 0 deletions decode-ways/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
func numDecodings(_ s: String) -> Int {
var array = Array(s)
var dp: [Int: Int] = [array.count: 1]
for i in stride(from: array.count - 1, to: -1, by: -1) {
if array[i] == "0" {
dp[i] = 0
} else {
dp[i] = dp[i + 1]
}

if i + 1 < array.count && (array[i] == "1" || array[i] == "2" && "0123456".contains(array[i + 1])) {
dp[i, default: 0] += dp[i + 2] ?? 0
}
}
return dp[0]!
}
}

12 changes: 12 additions & 0 deletions maximum-subarray/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
func maxSubArray(_ nums: [Int]) -> Int {
var maxSum = nums[0]
var maxEndingHere = nums[0]
for i in 1..<nums.count {
maxEndingHere = max(nums[i], maxEndingHere + nums[i])
maxSum = max(maxSum, maxEndingHere)
}
return maxSum
}
}

14 changes: 14 additions & 0 deletions number-of-1-bits/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
func hammingWeight(_ n: Int) -> Int {
var number = n
var answer = 0
while number > 0 {
if number & 1 == 1 {
answer += 1
}
number = number >> 1
}
return answer
}
}

29 changes: 29 additions & 0 deletions valid-palindrome/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
func isPalindrome(_ s: String) -> Bool {
let string = s.replacingOccurrences(of: " ", with: "").lowercased().map { $0 }
var leftIndex = string.startIndex
var rightIndex = string.endIndex - 1
let letterRange: ClosedRange<UInt8> = 97...122
let numberRange: ClosedRange<UInt8> = 48...57
while leftIndex <= rightIndex {
guard let leftCharAscii = string[leftIndex].asciiValue else { break }
guard let rightCharAscii = string[rightIndex].asciiValue else { break }
if !letterRange.contains(leftCharAscii) && !numberRange.contains(leftCharAscii) {
leftIndex += 1
continue
}
if !letterRange.contains(rightCharAscii) && !numberRange.contains(rightCharAscii) {
rightIndex -= 1
continue
}
if leftCharAscii == rightCharAscii {
leftIndex += 1
rightIndex -= 1
} else {
return false
}
}
return true
}
}