Skip to content

Commit

Permalink
feat: add swift solution to lcci problems: No.01.01,01.03 (#2564)
Browse files Browse the repository at this point in the history
  • Loading branch information
klever34 authored Apr 9, 2024
1 parent f16d75a commit 594929f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lcci/01.01.Is Unique/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ var isUnique = function (astr) {
};
```

```swift
class Solution {
func isUnique(_ astr: String) -> Bool {
var mask = 0
for c in astr {
let i = Int(c.asciiValue! - Character("a").asciiValue!)
if (mask >> i) & 1 != 0 {
return false
}
mask |= 1 << i
}
return true
}
}
```

<!-- tabs:end -->

<!-- end -->
16 changes: 16 additions & 0 deletions lcci/01.01.Is Unique/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ var isUnique = function (astr) {
};
```

```swift
class Solution {
func isUnique(_ astr: String) -> Bool {
var mask = 0
for c in astr {
let i = Int(c.asciiValue! - Character("a").asciiValue!)
if (mask >> i) & 1 != 0 {
return false
}
mask |= 1 << i
}
return true
}
}
```

<!-- tabs:end -->

<!-- end -->
13 changes: 13 additions & 0 deletions lcci/01.01.Is Unique/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
func isUnique(_ astr: String) -> Bool {
var mask = 0
for c in astr {
let i = Int(c.asciiValue! - Character("a").asciiValue!)
if (mask >> i) & 1 != 0 {
return false
}
mask |= 1 << i
}
return true
}
}
19 changes: 19 additions & 0 deletions lcci/01.03.String to URL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ var replaceSpaces = function (S, length) {
};
```

```swift
class Solution {
func replaceSpaces(_ S: String, _ length: Int) -> String {
let substring = S.prefix(length)
var result = ""

for character in substring {
if character == " " {
result += "%20"
} else {
result.append(character)
}
}

return result
}
}
```

<!-- tabs:end -->

### 方法二:模拟
Expand Down
19 changes: 19 additions & 0 deletions lcci/01.03.String to URL/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ var replaceSpaces = function (S, length) {
};
```

```swift
class Solution {
func replaceSpaces(_ S: String, _ length: Int) -> String {
let substring = S.prefix(length)
var result = ""

for character in substring {
if character == " " {
result += "%20"
} else {
result.append(character)
}
}

return result
}
}
```

<!-- tabs:end -->

### Solution 2: Simulation
Expand Down
16 changes: 16 additions & 0 deletions lcci/01.03.String to URL/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
func replaceSpaces(_ S: String, _ length: Int) -> String {
let substring = S.prefix(length)
var result = ""

for character in substring {
if character == " " {
result += "%20"
} else {
result.append(character)
}
}

return result
}
}

0 comments on commit 594929f

Please sign in to comment.