File tree Expand file tree Collapse file tree 5 files changed +86
-0
lines changed Expand file tree Collapse file tree 5 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ func combinationSum( _ candidates: [ Int ] , _ target: Int ) -> [ [ Int ] ] {
3+ if candidates. filter { $0 > target } . count == candidates. count {
4+ return [ ]
5+ }
6+
7+ var result : [ [ Int ] ] = [ ]
8+ var current : [ Int ] = [ ]
9+
10+ func backtrack( _ start: Int , _ remain: Int ) {
11+ if remain == 0 {
12+ result. append ( current)
13+ return
14+ }
15+
16+ if remain < 0 {
17+ return
18+ }
19+
20+ for i in start..< candidates. count {
21+ current. append ( candidates [ i] )
22+ backtrack ( i, remain - candidates[ i] )
23+ current. removeLast ( )
24+ }
25+ }
26+
27+ backtrack ( 0 , target)
28+
29+ return result
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func numDecodings( _ s: String ) -> Int {
3+ if s. first == " 0 " {
4+ return 0
5+ }
6+
7+ if s. count == 1 {
8+ return 1
9+ }
10+
11+ var tables = Array ( repeating: 0 , count: s. count+ 1 )
12+ let chars = Array ( s)
13+ tables [ 0 ] = 1
14+ tables [ 1 ] = 1
15+
16+ for i in 2 ... s. count {
17+ if chars [ i- 1 ] != " 0 " {
18+ tables [ i] += tables [ i- 1 ]
19+ }
20+
21+ if let twoDigit = Int ( String ( chars [ i- 2 ... i- 1 ] ) ) {
22+ if 10 <= twoDigit && twoDigit <= 26 {
23+ tables [ i] += tables [ i- 2 ]
24+ }
25+ }
26+
27+ }
28+
29+ return tables [ s. count]
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func maxSubArray( _ nums: [ Int ] ) -> Int {
3+ var currentSum = nums [ 0 ]
4+ var maxSum = nums [ 0 ]
5+
6+ for i in 1 ..< nums. count {
7+ currentSum = max ( nums [ i] , currentSum + nums[ i] )
8+ maxSum = max ( maxSum, currentSum)
9+ }
10+ return maxSum
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func hammingWeight( _ n: Int ) -> Int {
3+ let bitString = String ( n, radix: 2 )
4+ return bitString. filter { $0 == " 1 " } . count
5+ }
6+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func isPalindrome( _ s: String ) -> Bool {
3+ let strings = s. filter { $0. isLetter || $0. isNumber } . lowercased ( )
4+ return String ( strings. reversed ( ) ) == strings
5+ }
6+ }
You can’t perform that action at this time.
0 commit comments