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 6e6ffc6

Browse files
Added new solutions
1 parent 04e3816 commit 6e6ffc6

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// LeetCode: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
2+
3+
class Solution {
4+
func lengthOfLongestSubstring(_ s: String) -> Int {
5+
guard s.count > 0 else {
6+
return 0
7+
}
8+
9+
var maxCount = 1
10+
if s.count == 1 {
11+
return maxCount
12+
}
13+
let strArr = Array(s) // Key: convert string to array
14+
for (index, char) in strArr.enumerated() {
15+
var dict: [Character : Int] = [:]
16+
var count = 1
17+
dict[char] = 1
18+
var movingIdx = index + 1
19+
while movingIdx < strArr.count, dict[strArr[movingIdx]] ?? 0 < 1 {
20+
dict[strArr[movingIdx]] = 1
21+
count += 1
22+
movingIdx += 1
23+
}
24+
maxCount = count > maxCount ? count : maxCount
25+
}
26+
return maxCount
27+
}
28+
}
29+
30+
let s = Solution()
31+
print("\(s.lengthOfLongestSubstring("dvdf"))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// LeetCode: https://leetcode.com/problems/zigzag-conversion/description/
2+
3+
class Solution {
4+
func convert(_ s: String, _ numRows: Int) -> String {
5+
var output = ""
6+
let divide = 2 * numRows - 2
7+
guard divide > 1 else {
8+
return s
9+
}
10+
var dict: [Int:String] = [:]
11+
12+
for (index, char) in s.enumerated() {
13+
let dictIdx = (index % divide) < numRows ? (index % divide) + 1 : numRows - ((index % divide) + 1 - numRows)
14+
if nil != dict[dictIdx] {
15+
dict[dictIdx]!.append(char)
16+
} else {
17+
dict[dictIdx] = String(char)
18+
}
19+
}
20+
for i in 1...dict.count {
21+
output.append(dict[i]!)
22+
}
23+
return output
24+
}
25+
}
26+
27+
let solution = Solution()
28+
print("\(solution.convert("PAYPALISHIRING", 3))")
29+
print("\(solution.convert("PAYPALISHIRING", 4))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// LeetCode: https://leetcode.com/problems/reverse-linked-list/description/
2+
3+
public class ListNode {
4+
public var val: Int
5+
public var next: ListNode?
6+
public init(_ val: Int) {
7+
self.val = val
8+
self.next = nil
9+
}
10+
11+
public init(_ val: Int, _ next: ListNode?) {
12+
self.val = val
13+
self.next = next
14+
}
15+
}
16+
17+
class Solution {
18+
func reverseList(_ head: ListNode?) -> ListNode? {
19+
guard let head = head else {
20+
return nil
21+
}
22+
var currentNode: ListNode? = head
23+
var prev: ListNode? = nil // The previous node of head is nil
24+
while nil != currentNode {
25+
let next: ListNode? = currentNode?.next
26+
currentNode?.next = prev
27+
prev = currentNode
28+
currentNode = next
29+
}
30+
return prev
31+
}
32+
33+
func printLinkedList(_ head: ListNode) {
34+
var printedStr = ""
35+
var currentNode: ListNode? = head
36+
while nil != currentNode {
37+
printedStr.append("\(currentNode?.val ?? -1)")
38+
if nil != currentNode?.next {
39+
printedStr.append(", ")
40+
}
41+
currentNode = currentNode?.next
42+
}
43+
print(printedStr)
44+
}
45+
}
46+
47+
let solution = Solution()
48+
let node5 = ListNode(5, nil)
49+
let node4 = ListNode(4, node5)
50+
let node3 = ListNode(3, node4)
51+
let node2 = ListNode(2, node3)
52+
let node1 = ListNode(1, node2)
53+
solution.printLinkedList(node1)
54+
55+
// Reverse Linked List
56+
print("Reversed below:")
57+
solution.printLinkedList(solution.reverseList(node1)!)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
(0)

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