File tree Expand file tree Collapse file tree 6 files changed +176
-0
lines changed Expand file tree Collapse file tree 6 files changed +176
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,33 @@ var CheckPermutation = function (s1, s2) {
170
170
};
171
171
```
172
172
173
+ ``` swift
174
+ class Solution {
175
+ func CheckPermutation (_ s1 : String , _ s2 : String ) -> Bool {
176
+ if s1.count != s2.count {
177
+ return false
178
+ }
179
+
180
+ var cnt = Array (repeating : 0 , count : 26 )
181
+
182
+ for char in s1 {
183
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
184
+ cnt[index] += 1
185
+ }
186
+
187
+ for char in s2 {
188
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
189
+ cnt[index] -= 1
190
+ if cnt[index] < 0 {
191
+ return false
192
+ }
193
+ }
194
+
195
+ return true
196
+ }
197
+ }
198
+ ```
199
+
173
200
<!-- tabs: end -->
174
201
175
202
### 方法二:排序
Original file line number Diff line number Diff line change @@ -242,6 +242,33 @@ impl Solution {
242
242
}
243
243
```
244
244
245
+ ``` swift
246
+ class Solution {
247
+ func CheckPermutation (_ s1 : String , _ s2 : String ) -> Bool {
248
+ if s1.count != s2.count {
249
+ return false
250
+ }
251
+
252
+ var cnt = Array (repeating : 0 , count : 26 )
253
+
254
+ for char in s1 {
255
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
256
+ cnt[index] += 1
257
+ }
258
+
259
+ for char in s2 {
260
+ let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
261
+ cnt[index] -= 1
262
+ if cnt[index] < 0 {
263
+ return false
264
+ }
265
+ }
266
+
267
+ return true
268
+ }
269
+ }
270
+ ```
271
+
245
272
<!-- tabs: end -->
246
273
247
274
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func CheckPermutation( _ s1: String , _ s2: String ) -> Bool {
3
+ if s1. count != s2. count {
4
+ return false
5
+ }
6
+
7
+ var cnt = Array ( repeating: 0 , count: 26 )
8
+
9
+ for char in s1 {
10
+ let index = Int ( char. asciiValue! - Character( " a " ) . asciiValue!)
11
+ cnt [ index] += 1
12
+ }
13
+
14
+ for char in s2 {
15
+ let index = Int ( char. asciiValue! - Character( " a " ) . asciiValue!)
16
+ cnt [ index] -= 1
17
+ if cnt [ index] < 0 {
18
+ return false
19
+ }
20
+ }
21
+
22
+ return true
23
+ }
24
+ }
Original file line number Diff line number Diff line change @@ -258,6 +258,40 @@ var addTwoNumbers = function (l1, l2) {
258
258
};
259
259
` ` `
260
260
261
+ ` ` ` swift
262
+ /**
263
+ * Definition for singly-linked list.
264
+ * class ListNode {
265
+ * var val: Int
266
+ * var next: ListNode?
267
+ * init(_ val: Int) {
268
+ * self.val = val
269
+ * self.next = nil
270
+ * }
271
+ * }
272
+ */
273
+
274
+ class Solution {
275
+ func addTwoNumbers (_ l1: ListNode? , _ l2: ListNode? ) - > ListNode? {
276
+ var carry = 0
277
+ let dummy = ListNode (0 )
278
+ var current: ListNode? = dummy
279
+ var l1 = l1, l2 = l2
280
+
281
+ while l1 != nil || l2 != nil || carry != 0 {
282
+ let sum = (l1? .val ?? 0 ) + (l2? .val ?? 0 ) + carry
283
+ carry = sum / 10
284
+ current? .next = ListNode (sum % 10 )
285
+ current = current? .next
286
+ l1 = l1? .next
287
+ l2 = l2? .next
288
+ }
289
+
290
+ return dummy .next
291
+ }
292
+ }
293
+ ` ` `
294
+
261
295
<!-- tabs:end -->
262
296
263
297
<!-- end -->
Original file line number Diff line number Diff line change @@ -261,6 +261,40 @@ var addTwoNumbers = function (l1, l2) {
261
261
};
262
262
` ` `
263
263
264
+ ` ` ` swift
265
+ /**
266
+ * Definition for singly-linked list.
267
+ * class ListNode {
268
+ * var val: Int
269
+ * var next: ListNode?
270
+ * init(_ val: Int) {
271
+ * self.val = val
272
+ * self.next = nil
273
+ * }
274
+ * }
275
+ */
276
+
277
+ class Solution {
278
+ func addTwoNumbers (_ l1: ListNode? , _ l2: ListNode? ) - > ListNode? {
279
+ var carry = 0
280
+ let dummy = ListNode (0 )
281
+ var current: ListNode? = dummy
282
+ var l1 = l1, l2 = l2
283
+
284
+ while l1 != nil || l2 != nil || carry != 0 {
285
+ let sum = (l1? .val ?? 0 ) + (l2? .val ?? 0 ) + carry
286
+ carry = sum / 10
287
+ current? .next = ListNode (sum % 10 )
288
+ current = current? .next
289
+ l1 = l1? .next
290
+ l2 = l2? .next
291
+ }
292
+
293
+ return dummy .next
294
+ }
295
+ }
296
+ ` ` `
297
+
264
298
<!-- tabs:end -->
265
299
266
300
<!-- end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
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
+ */
12
+ class Solution {
13
+ func addTwoNumbers( _ l1: ListNode ? , _ l2: ListNode ? ) -> ListNode ? {
14
+ var carry = 0
15
+ let dummy = ListNode ( 0 )
16
+ var current : ListNode ? = dummy
17
+ var l1 = l1, l2 = l2
18
+
19
+ while l1 != nil || l2 != nil || carry != 0 {
20
+ let sum = ( l1? . val ?? 0 ) + ( l2? . val ?? 0 ) + carry
21
+ carry = sum / 10
22
+ current? . next = ListNode ( sum % 10 )
23
+ current = current? . next
24
+ l1 = l1? . next
25
+ l2 = l2? . next
26
+ }
27
+
28
+ return dummy. next
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments