File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) {
370
370
}
371
371
```
372
372
373
-
373
+ Scala:
374
+ ```scala
375
+ object Solution {
376
+ def detectCycle(head: ListNode): ListNode = {
377
+ var fast = head // 快指针
378
+ var slow = head // 慢指针
379
+ while (fast != null && fast.next != null) {
380
+ fast = fast.next.next // 快指针一次走两步
381
+ slow = slow.next // 慢指针一次走一步
382
+ // 如果相遇,fast快指针回到头
383
+ if (fast == slow) {
384
+ fast = head
385
+ // 两个指针一步一步的走,第一次相遇的节点必是入环节点
386
+ while (fast != slow) {
387
+ fast = fast.next
388
+ slow = slow.next
389
+ }
390
+ return fast
391
+ }
392
+ }
393
+ // 如果fast指向空值,必然无环返回null
394
+ null
395
+ }
396
+ }
397
+ ```
374
398
375
399
-----------------------
376
400
<div align =" center " ><img src =https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width =500 > </img ></div >
Original file line number Diff line number Diff line change @@ -308,6 +308,32 @@ impl Solution {
308
308
}
309
309
```
310
310
311
+
312
+ Scala:
313
+ ``` scala
314
+ object Solution {
315
+ def isAnagram (s : String , t : String ): Boolean = {
316
+ // 如果两个字符串的长度不等,直接返回false
317
+ if (s.length != t.length) return false
318
+ val record = new Array [Int ](26 ) // 记录每个单词出现了多少次
319
+ // 遍历字符串,对于s字符串单词对应的记录+=1,t字符串对应的记录-=1
320
+ for (i <- 0 until s.length) {
321
+ record(s(i) - 97 ) += 1
322
+ record(t(i) - 97 ) -= 1
323
+ }
324
+ // 如果不等于则直接返回false
325
+ for (i <- 0 until 26 ) {
326
+ if (record(i) != 0 ) {
327
+ return false
328
+ }
329
+ }
330
+ // 如果前面不返回false,说明匹配成功,返回true,return可以省略
331
+ true
332
+ }
333
+ }
334
+ ```
335
+
336
+
311
337
C#:
312
338
``` csharp
313
339
public bool IsAnagram (string s , string t ) {
326
352
return true ;
327
353
}
328
354
```
355
+
329
356
## 相关题目
330
357
331
358
* 383.赎金信
You can’t perform that action at this time.
0 commit comments