Skip to content

Commit 8ec2c3c

Browse files
committed
Update
1 parent 831fe54 commit 8ec2c3c

File tree

1,434 files changed

+13070
-12960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,434 files changed

+13070
-12960
lines changed

0001-0500/0002. Add Two Numbers.swift

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* public class ListNode {
4-
* public var val: Int
5-
* public var next: ListNode?
6-
* public init() { self.val = 0; self.next = nil; }
7-
* public init(_ val: Int) { self.val = val; self.next = nil; }
8-
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9-
* }
10-
*/
1+
/// Definition for singly-linked list.
2+
/// public class ListNode {
3+
/// public var val: Int
4+
/// public var next: ListNode?
5+
/// public init() { self.val = 0; self.next = nil; }
6+
/// public init(_ val: Int) { self.val = val; self.next = nil; }
7+
/// public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
8+
/// }
119

1210
class Solution {
1311

@@ -21,15 +19,15 @@ class Solution {
2119
// Input: l1 = [2,4,3], l2 = [5,6,4]
2220
// Output: [7,0,8]
2321
// Explanation: 342 + 465 = 807.
24-
22+
2523
// Example 2:
2624
// Input: l1 = [0], l2 = [0]
2725
// Output: [0]
2826

2927
// Example 3:
3028
// Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
3129
// Output: [8,9,9,9,0,0,0,1]
32-
30+
3331
// Constraints:
3432
// The number of nodes in each linked list is in the range [1, 100].
3533
// 0 <= Node.val <= 9
@@ -45,34 +43,34 @@ class Solution {
4543
// - Complexity:
4644
// - time: O(max(n, m)), where n is the length of the l1 and m is the length of the l2.
4745
// - space: O(max(n, m)), where n is the length of the l1 and m is the length of the l2.
48-
46+
4947
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
5048
var l1 = l1
5149
var l2 = l2
5250
let dummyHead = ListNode(-1)
5351
var tail: ListNode? = dummyHead
5452
var carry = 0
55-
53+
5654
while l1 != nil || l2 != nil || carry != 0 {
5755
var value = carry
58-
56+
5957
if let l1Val = l1?.val {
6058
value += l1Val
6159
l1 = l1?.next
6260
}
63-
61+
6462
if let l2Val = l2?.val {
6563
value += l2Val
6664
l2 = l2?.next
6765
}
68-
66+
6967
let digit = value % 10
7068
carry = value / 10
7169
tail?.next = ListNode(digit)
7270
tail = tail?.next
7371
}
74-
72+
7573
return dummyHead.next
7674
}
7775

78-
}
76+
}

0001-0500/0003. Longest Substring Without Repeating Characters.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ class Solution {
99
// Input: s = "abcabcbb"
1010
// Output: 3
1111
// Explanation: The answer is "abc", with the length of 3.
12-
12+
1313
// Example 2:
1414
// Input: s = "bbbbb"
1515
// Output: 1
1616
// Explanation: The answer is "b", with the length of 1.
17-
17+
1818
// Example 3:
1919
// Input: s = "pwwkew"
2020
// Output: 3
2121
// Explanation: The answer is "wke", with the length of 3.
2222
// Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
23-
23+
2424
// Example 4:
2525
// Input: s = ""
2626
// Output: 0
27-
27+
2828
// Constraints:
2929
// 0 <= s.length <= 5 * 10^4
3030
// s consists of English letters, digits, symbols and spaces.
@@ -37,15 +37,17 @@ class Solution {
3737
var string = Array(s)
3838
var chars: [Character] = []
3939
let length = s.count
40-
40+
4141
for i in 0..<length {
4242
let char = string[i]
43-
if let same_char_index = chars.firstIndex(of: char) { chars = Array(chars[(same_char_index+1)..<chars.count]) }
43+
if let same_char_index = chars.firstIndex(of: char) {
44+
chars = Array(chars[(same_char_index + 1)..<chars.count])
45+
}
4446
chars.append(char)
4547
max = chars.count > max ? chars.count : max
4648
}
47-
49+
4850
return max
4951
}
5052

51-
}
53+
}

0001-0500/0004. Median of Two Sorted Arrays.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ class Solution {
7373

7474
return res
7575
}
76-
77-
}
76+
77+
}

0001-0500/0005. Longest Palindromic Substring.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Solution {
2121
// Example 4:
2222
// Input: s = "ac"
2323
// Output: "a"
24-
24+
2525
// Constraints:
2626
// 1 <= s.length <= 1000
2727
// s consist of only digits and English letters (lower-case and/or upper-case)
@@ -38,8 +38,8 @@ class Solution {
3838
for i in 0..<count {
3939
var tmp = 0
4040
for j in 0..<count {
41-
if (i - j < 0 || i + j > count-1) { break }
42-
if (string[i - j] != string[i + j]) { break }
41+
if i - j < 0 || i + j > count - 1 { break }
42+
if string[i - j] != string[i + j] { break }
4343
max1 = max1 > tmp ? max1 : tmp
4444
index1 = max1 > tmp ? index1 : i
4545
tmp += 1
@@ -49,24 +49,23 @@ class Solution {
4949
for i in 0..<count {
5050
var tmp = 0
5151
for j in 0..<count {
52-
if (i - j < 0 || i + j + 1 > count - 1) { break }
53-
if (string[i - j] != string[i + j + 1]) { break }
52+
if i - j < 0 || i + j + 1 > count - 1 { break }
53+
if string[i - j] != string[i + j + 1] { break }
5454
tmp += 1
5555
max2 = max2 > tmp ? max2 : tmp
5656
index2 = max2 > tmp ? index2 : i
5757
}
5858
}
5959

60-
if ((max1 * 2 + 1) >= max2 * 2) {
60+
if (max1 * 2 + 1) >= max2 * 2 {
6161
let start = s.index(s.startIndex, offsetBy: index1 - max1)
6262
let end = s.index(s.startIndex, offsetBy: index1 + max1 + 1)
6363
return String(s[start..<end])
64-
}
65-
else {
64+
} else {
6665
let start = s.index(s.startIndex, offsetBy: index2 - max2 + 1)
6766
let end = s.index(s.startIndex, offsetBy: index2 + max2 + 1)
6867
return String(s[start..<end])
69-
}
68+
}
7069
}
7170

72-
}
71+
}

0001-0500/0006. ZigZag Conversion.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class Solution {
1111
// And then read line by line: "PAHNAPLSIIGYIR"
1212
// Write the code that will take a string and make this conversion given a number of rows:
1313
// string convert(string s, int numRows);
14-
14+
1515
// Example 1:
1616
// Input: s = "PAYPALISHIRING", numRows = 3
1717
// Output: "PAHNAPLSIIGYIR"
18-
18+
1919
// Example 2:
2020
// Input: s = "PAYPALISHIRING", numRows = 4
2121
// Output: "PINALSIGYAHRPI"
@@ -28,7 +28,7 @@ class Solution {
2828
// Example 3:
2929
// Input: s = "A", numRows = 1
3030
// Output: "A"
31-
31+
3232
// Constraints:
3333
// 1 <= s.length <= 1000
3434
// s consists of English letters (lower-case and upper-case), ',' and '.'.

0001-0500/0007. Reverse Integer.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Solution {
2121
// Example 4:
2222
// Input: x = 0
2323
// Output: 0
24-
24+
2525
// Constraints:
2626
// -2^31 <= x <= 2^31 - 1
2727

@@ -31,16 +31,16 @@ class Solution {
3131
var nums: [Int] = []
3232
let max: Double = pow(2, 31)
3333
var res: Int = 0
34-
34+
3535
while val > 0 {
3636
nums.append(val % 10)
3737
val = val / 10
3838
}
39-
39+
4040
for num in nums { res = 10 * res + num }
4141
res = isNegtive ? -res : res
42-
if res < -Int(max) || res > Int(max) - 1 { res = 0 }
42+
if res < -Int(max) || res > Int(max) - 1 { res = 0 }
4343
return res
4444
}
4545

46-
}
46+
}

0001-0500/0008. String to Integer (atoi).swift

+20-13
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class Solution {
1212
// Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
1313
// If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1.
1414
// Return the integer as the final result.
15-
15+
1616
// Note:
1717
// Only the space character ' ' is considered a whitespace character.
1818
// Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
19-
19+
2020
// Example 1:
2121
// Input: s = "42"
2222
// Output: 42
@@ -29,7 +29,7 @@ class Solution {
2929
// ^
3030
// The parsed integer is 42.
3131
// Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42.
32-
32+
3333
// Example 2:
3434
// Input: s = " -42"
3535
// Output: -42
@@ -42,7 +42,7 @@ class Solution {
4242
// ^
4343
// The parsed integer is -42.
4444
// Since -42 is in the range [-2^31, 2^31 - 1], the final result is -42.
45-
45+
4646
// Example 3:
4747
// Input: s = "4193 with words"
4848
// Output: 4193
@@ -55,7 +55,7 @@ class Solution {
5555
// ^
5656
// The parsed integer is 4193.
5757
// Since 4193 is in the range [-2^31, 2^31 - 1], the final result is 4193.
58-
58+
5959
// Example 4:
6060
// Input: s = "words and 987"
6161
// Output: 0
@@ -68,7 +68,7 @@ class Solution {
6868
// ^
6969
// The parsed integer is 0 because no digits were read.
7070
// Since 0 is in the range [-2^31, 2^31 - 1], the final result is 0.
71-
71+
7272
// Example 5:
7373
// Input: s = "-91283472332"
7474
// Output: -2147483648
@@ -81,7 +81,7 @@ class Solution {
8181
// ^
8282
// The parsed integer is -91283472332.
8383
// Since -91283472332 is less than the lower bound of the range [-2^31, 2^31 - 1], the final result is clamped to -2^31 = -2147483648.
84-
84+
8585
// Constraints:
8686
// 0 <= s.length <= 200
8787
// s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
@@ -101,8 +101,9 @@ class Solution {
101101
if isLeading == false { isValid = false }
102102
case "-", "+":
103103
isLeading = false
104-
if isSign || nums.count > 0 { isValid = false }
105-
else {
104+
if isSign || nums.count > 0 {
105+
isValid = false
106+
} else {
106107
isSign = true
107108
isNegtive = c == "-" ? true : false
108109
}
@@ -117,12 +118,18 @@ class Solution {
117118
for num in nums {
118119
if num == 0 && isLeadingZero { continue } else { isLeadingZero = false }
119120
res = num + 10 * res
120-
121-
if isNegtive && res > Int(pow(2.0, 31)) { res = Int(pow(2.0, 31)); break }
122-
if !isNegtive && res > Int(pow(2.0, 31)) - 1 { res = Int(pow(2.0, 31)) - 1; break }
121+
122+
if isNegtive && res > Int(pow(2.0, 31)) {
123+
res = Int(pow(2.0, 31))
124+
break
125+
}
126+
if !isNegtive && res > Int(pow(2.0, 31)) - 1 {
127+
res = Int(pow(2.0, 31)) - 1
128+
break
129+
}
123130
}
124131

125132
return isNegtive ? -1 * res : res
126133
}
127134

128-
}
135+
}

0 commit comments

Comments
 (0)