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 a06e6ef

Browse files
Added accepted solutions
1 parent 6e6ffc6 commit a06e6ef

File tree

10 files changed

+242
-0
lines changed

10 files changed

+242
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// LeetCode: https://leetcode.com/problems/palindrome-number/description/
2+
3+
class Solution {
4+
func isPalindrome(_ x: Int) -> Bool {
5+
guard x >= 0 else { return false }
6+
guard x >= 10 else { return true }
7+
8+
var num = x
9+
var reverse = 0
10+
while num != 0 {
11+
let temp = num % 10
12+
num = num / 10
13+
reverse = reverse * 10 + temp
14+
}
15+
16+
return reverse == x
17+
}
18+
}
19+
20+
let solution = Solution()
21+
print("\(solution.isPalindrome(121))")
22+
print("\(solution.isPalindrome(-121))")
23+
print("\(solution.isPalindrome(10))")
24+
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// LeetCode: https://leetcode.com/problems/integer-to-roman/description/
2+
/**
3+
Roman integers table
4+
1 : "I"
5+
5 : "V"
6+
10 : "X"
7+
50 : "L"
8+
100 : "C"
9+
500 : "D"
10+
1000 : "M"
11+
*/
12+
13+
class Solution {
14+
func intToRoman(_ num: Int) -> String {
15+
var output = ""
16+
output.append(thousands(num: num/1000))
17+
output.append(hundreds(num: num/100-10*(num/1000)))
18+
output.append(tens(num: num/10-10*(num/100)))
19+
output.append(ones(num: num/1-10*(num/10)))
20+
return output
21+
}
22+
23+
func thousands(num: Int) -> String {
24+
// Since the input range is 1-3999, mid and high doesn't matter
25+
return formatter(num: num, low: "M", mid: "", high: "")
26+
}
27+
28+
func hundreds(num: Int) -> String {
29+
return formatter(num: num, low: "C", mid: "D", high: "M")
30+
}
31+
32+
func tens(num: Int) -> String {
33+
return formatter(num: num, low: "X", mid: "L", high: "C")
34+
}
35+
36+
func ones(num: Int) -> String {
37+
return formatter(num: num, low: "I", mid: "V", high: "X")
38+
}
39+
40+
private func formatter(num: Int, low: String, mid: String, high: String) -> String {
41+
switch num {
42+
case 1:
43+
return low
44+
case 2:
45+
return low + low
46+
case 3:
47+
return low + low + low
48+
case 4:
49+
return low + mid
50+
case 5:
51+
return mid
52+
case 6:
53+
return mid + low
54+
case 7:
55+
return mid + low + low
56+
case 8:
57+
return mid + low + low + low
58+
case 9:
59+
return low + high
60+
default:
61+
return ""
62+
}
63+
}
64+
}
65+
66+
let solution = Solution()
67+
print("\(solution.intToRoman(3))")
68+
print("\(solution.intToRoman(4))")
69+
print("\(solution.intToRoman(9))")
70+
print("\(solution.intToRoman(58))")
71+
print("\(solution.intToRoman(1994))")
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// LeetCode: https://leetcode.com/problems/roman-to-integer/description/
2+
3+
class Solution {
4+
func romanToInt(_ s: String) -> Int {
5+
var output = 0
6+
var strArr = Array(s)
7+
for index in 0..<strArr.count {
8+
var numToAdd = mapToInt(strArr[index])
9+
if index+1 < strArr.count, mapToInt(strArr[index+1]) > mapToInt(strArr[index]) {
10+
numToAdd = numToAdd * -1
11+
}
12+
output += numToAdd
13+
}
14+
15+
return output
16+
}
17+
18+
private func mapToInt(_ char: Character) -> Int {
19+
switch char {
20+
case "M":
21+
return 1000
22+
case "D":
23+
return 500
24+
case "C":
25+
return 100
26+
case "L":
27+
return 50
28+
case "X":
29+
return 10
30+
case "V":
31+
return 5
32+
case "I":
33+
return 1
34+
default:
35+
return 0
36+
}
37+
}
38+
}
39+
40+
let solution = Solution()
41+
print("\(solution.romanToInt("III"))")
42+
print("\(solution.romanToInt("IV"))")
43+
print("\(solution.romanToInt("IX"))")
44+
print("\(solution.romanToInt("LVIII"))")
45+
print("\(solution.romanToInt("MCMXCIV"))")
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// LeetCode: https://leetcode.com/problems/longest-common-prefix/description/
2+
3+
class Solution {
4+
func longestCommonPrefix(_ strs: [String]) -> String {
5+
guard strs.count > 0 else {
6+
return ""
7+
}
8+
var output = Array(strs[0])
9+
for str in strs {
10+
if str == "" { return "" } // Handle edge case
11+
var strArr = Array(str)
12+
for i in 0..<strArr.count {
13+
output = output.count > strArr.count ? Array(String(output[0..<strArr.count])) : output
14+
if i < output.count, output[i] != strArr[i] {
15+
output = i > 0 ? Array(String(output[0..<i])) : Array("")
16+
}
17+
}
18+
}
19+
return String(output)
20+
}
21+
}
22+
23+
let solution = Solution()
24+
print("\(solution.longestCommonPrefix(["flower","flow","flight"]))")
25+
print("\(solution.longestCommonPrefix(["dog","racecar","car"]))")
26+
print("\(solution.longestCommonPrefix(["aa","a"]))")
27+
print("\(solution.longestCommonPrefix(["aba","ab",""]))")
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// LeetCode: https://leetcode.com/problems/merge-two-sorted-lists/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+
class Solution {
12+
func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
13+
if nil != l1, nil == l2 {
14+
return l1
15+
} else if nil == l1, nil != l2 {
16+
return l2
17+
}
18+
19+
guard let l1 = l1, let l2 = l2 else {
20+
return nil
21+
}
22+
var head: ListNode?
23+
var output: ListNode?
24+
var list1: ListNode? = l1
25+
var list2: ListNode? = l2
26+
if (list1?.val)! < (list2?.val)! {
27+
head = l1
28+
list1 = list1?.next
29+
} else {
30+
head = l2
31+
list2 = list2?.next
32+
}
33+
output = head
34+
35+
while nil != list1 || nil != list2 {
36+
guard nil != list1 else {
37+
head?.next = list2
38+
return output
39+
}
40+
guard nil != list2 else {
41+
head?.next = list1
42+
return output
43+
}
44+
if (list1?.val)! < (list2?.val)! {
45+
head?.next = list1
46+
list1 = list1?.next
47+
} else {
48+
head?.next = list2
49+
list2 = list2?.next
50+
}
51+
head = head?.next
52+
}
53+
return output
54+
}
55+
}
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='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
(0)

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