Skip to content

Commit d742058

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 9ba96f4 + 2c1aa6e commit d742058

File tree

61 files changed

+2836
-96
lines changed

Some content is hidden

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

61 files changed

+2836
-96
lines changed

problems/0001.两数之和.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ class Solution {
274274
}
275275
}
276276
```
277+
278+
Scala:
279+
```scala
280+
object Solution {
281+
// 导入包
282+
import scala.collection.mutable
283+
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
284+
// key存储值,value存储下标
285+
val map = new mutable.HashMap[Int, Int]()
286+
for (i <- nums.indices) {
287+
val tmp = target - nums(i) // 计算差值
288+
// 如果这个差值存在于map,则说明找到了结果
289+
if (map.contains(tmp)) {
290+
return Array(map.get(tmp).get, i)
291+
}
292+
// 如果不包含把当前值与其下标放到map
293+
map.put(nums(i), i)
294+
}
295+
// 如果没有找到直接返回一个空的数组,return关键字可以省略
296+
new Array[Int](2)
297+
}
298+
}
299+
```
300+
277301
C#:
278302
```csharp
279303
public class Solution {
@@ -293,5 +317,6 @@ public class Solution {
293317
}
294318
```
295319

320+
296321
-----------------------
297322
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0015.三数之和.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,49 @@ public class Solution
616616
}
617617
}
618618
```
619-
619+
Scala:
620+
```scala
621+
object Solution {
622+
// 导包
623+
import scala.collection.mutable.ListBuffer
624+
import scala.util.control.Breaks.{break, breakable}
625+
626+
def threeSum(nums: Array[Int]): List[List[Int]] = {
627+
// 定义结果集,最后需要转换为List
628+
val res = ListBuffer[List[Int]]()
629+
val nums_tmp = nums.sorted // 对nums进行排序
630+
for (i <- nums_tmp.indices) {
631+
// 如果要排的第一个数字大于0,直接返回结果
632+
if (nums_tmp(i) > 0) {
633+
return res.toList
634+
}
635+
// 如果i大于0并且和前一个数字重复,则跳过本次循环,相当于continue
636+
breakable {
637+
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
638+
break
639+
} else {
640+
var left = i + 1
641+
var right = nums_tmp.length - 1
642+
while (left < right) {
643+
var sum = nums_tmp(i) + nums_tmp(left) + nums_tmp(right) // 求三数之和
644+
if (sum < 0) left += 1
645+
else if (sum > 0) right -= 1
646+
else {
647+
res += List(nums_tmp(i), nums_tmp(left), nums_tmp(right)) // 如果等于0 添加进结果集
648+
// 为了避免重复,对left和right进行移动
649+
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
650+
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
651+
left += 1
652+
right -= 1
653+
}
654+
}
655+
}
656+
}
657+
}
658+
// 最终返回需要转换为List,return关键字可以省略
659+
res.toList
660+
}
661+
}
662+
```
620663
-----------------------
621664
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0018.四数之和.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,49 @@ public class Solution
522522
}
523523
}
524524
```
525-
525+
Scala:
526+
```scala
527+
object Solution {
528+
// 导包
529+
import scala.collection.mutable.ListBuffer
530+
import scala.util.control.Breaks.{break, breakable}
531+
def fourSum(nums: Array[Int], target: Int): List[List[Int]] = {
532+
val res = ListBuffer[List[Int]]()
533+
val nums_tmp = nums.sorted // 先排序
534+
for (i <- nums_tmp.indices) {
535+
breakable {
536+
if (i > 0 && nums_tmp(i) == nums_tmp(i - 1)) {
537+
break // 如果该值和上次的值相同,跳过本次循环,相当于continue
538+
} else {
539+
for (j <- i + 1 until nums_tmp.length) {
540+
breakable {
541+
if (j > i + 1 && nums_tmp(j) == nums_tmp(j - 1)) {
542+
break // 同上
543+
} else {
544+
// 双指针
545+
var (left, right) = (j + 1, nums_tmp.length - 1)
546+
while (left < right) {
547+
var sum = nums_tmp(i) + nums_tmp(j) + nums_tmp(left) + nums_tmp(right)
548+
if (sum == target) {
549+
// 满足要求,直接加入到集合里面去
550+
res += List(nums_tmp(i), nums_tmp(j), nums_tmp(left), nums_tmp(right))
551+
while (left < right && nums_tmp(left) == nums_tmp(left + 1)) left += 1
552+
while (left < right && nums_tmp(right) == nums_tmp(right - 1)) right -= 1
553+
left += 1
554+
right -= 1
555+
} else if (sum < target) left += 1
556+
else right -= 1
557+
}
558+
}
559+
}
560+
}
561+
}
562+
}
563+
}
564+
// 最终返回的res要转换为List,return关键字可以省略
565+
res.toList
566+
}
567+
}
568+
```
526569
-----------------------
527570
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0020.有效的括号.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,27 @@ bool isValid(char * s){
400400
return !stackTop;
401401
}
402402
```
403-
403+
Scala:
404+
```scala
405+
object Solution {
406+
import scala.collection.mutable
407+
def isValid(s: String): Boolean = {
408+
if(s.length % 2 != 0) return false // 如果字符串长度是奇数直接返回false
409+
val stack = mutable.Stack[Char]()
410+
// 循环遍历字符串
411+
for (i <- s.indices) {
412+
val c = s(i)
413+
if (c == '(' || c == '[' || c == '{') stack.push(c)
414+
else if(stack.isEmpty) return false // 如果没有(、[、{则直接返回false
415+
// 以下三种情况,不满足则直接返回false
416+
else if(c==')' && stack.pop() != '(') return false
417+
else if(c==']' && stack.pop() != '[') return false
418+
else if(c=='}' && stack.pop() != '{') return false
419+
}
420+
// 如果为空则正确匹配,否则还有余孽就不匹配
421+
stack.isEmpty
422+
}
423+
}
424+
```
404425
-----------------------
405426
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0027.移除元素.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
## 思路
3030

31+
[本题B站视频讲解](https://www.bilibili.com/video/BV12A4y1Z7LP)
32+
3133
有的同学可能说了,多余的元素,删掉不就得了。
3234

3335
**要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。**
@@ -75,10 +77,20 @@ public:
7577

7678
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
7779

80+
定义快慢指针
81+
82+
* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
83+
* 慢指针:指向更新 新数组下标的位置
84+
85+
很多同学这道题目做的很懵,就是不理解 快慢指针究竟都是什么含义,所以一定要明确含义,后面的思路就更容易理解了。
86+
7887
删除过程如下:
7988

8089
![27.移除元素-双指针法](https://tva1.sinaimg.cn/large/008eGmZEly1gntrds6r59g30du09mnpd.gif)
8190

91+
很多同学不了解
92+
93+
8294
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
8395

8496
后续都会一一介绍到,本题代码如下:
@@ -104,8 +116,6 @@ public:
104116
* 时间复杂度:O(n)
105117
* 空间复杂度:O(1)
106118
107-
旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)
108-
109119
```CPP
110120
/**
111121
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
@@ -329,5 +339,34 @@ int removeElement(int* nums, int numsSize, int val){
329339
}
330340
```
331341
342+
343+
Kotlin:
344+
```kotlin
345+
fun removeElement(nums: IntArray, `val`: Int): Int {
346+
var slowIndex = 0 // 初始化慢指针
347+
for (fastIndex in nums.indices) {
348+
if (nums[fastIndex] != `val`) nums[slowIndex++] = nums[fastIndex] // 在慢指针所在位置存储未被删除的元素
349+
}
350+
return slowIndex
351+
}
352+
```
353+
354+
355+
Scala:
356+
```scala
357+
object Solution {
358+
def removeElement(nums: Array[Int], `val`: Int): Int = {
359+
var slow = 0
360+
for (fast <- 0 until nums.length) {
361+
if (`val` != nums(fast)) {
362+
nums(slow) = nums(fast)
363+
slow += 1
364+
}
365+
}
366+
slow
367+
}
368+
}
369+
```
370+
332371
-----------------------
333372
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0028.实现strStr.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,5 +1166,80 @@ func strStr(_ haystack: String, _ needle: String) -> Int {
11661166

11671167
```
11681168

1169+
PHP:
1170+
1171+
> 前缀表统一减一
1172+
```php
1173+
function strStr($haystack, $needle) {
1174+
if (strlen($needle) == 0) return 0;
1175+
$next= [];
1176+
$this->getNext($next,$needle);
1177+
1178+
$j = -1;
1179+
for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始
1180+
while($j >= 0 && $haystack[$i] != $needle[$j + 1]) {
1181+
$j = $next[$j];
1182+
}
1183+
if ($haystack[$i] == $needle[$j + 1]) {
1184+
$j++;
1185+
}
1186+
if ($j == (strlen($needle) - 1) ) {
1187+
return ($i - strlen($needle) + 1);
1188+
}
1189+
}
1190+
return -1;
1191+
}
1192+
1193+
function getNext(&$next, $s){
1194+
$j = -1;
1195+
$next[0] = $j;
1196+
for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始
1197+
while ($j >= 0 && $s[$i] != $s[$j + 1]) {
1198+
$j = $next[$j];
1199+
}
1200+
if ($s[$i] == $s[$j + 1]) {
1201+
$j++;
1202+
}
1203+
$next[$i] = $j;
1204+
}
1205+
}
1206+
```
1207+
1208+
> 前缀表统一不减一
1209+
```php
1210+
function strStr($haystack, $needle) {
1211+
if (strlen($needle) == 0) return 0;
1212+
$next= [];
1213+
$this->getNext($next,$needle);
1214+
1215+
$j = 0;
1216+
for ($i = 0;$i < strlen($haystack); $i++) { // 注意i就从0开始
1217+
while($j > 0 && $haystack[$i] != $needle[$j]) {
1218+
$j = $next[$j-1];
1219+
}
1220+
if ($haystack[$i] == $needle[$j]) {
1221+
$j++;
1222+
}
1223+
if ($j == strlen($needle)) {
1224+
return ($i - strlen($needle) + 1);
1225+
}
1226+
}
1227+
return -1;
1228+
}
1229+
1230+
function getNext(&$next, $s){
1231+
$j = 0;
1232+
$next[0] = $j;
1233+
for($i = 1; $i < strlen($s); $i++) { // 注意i从1开始
1234+
while ($j > 0 && $s[$i] != $s[$j]) {
1235+
$j = $next[$j-1];
1236+
}
1237+
if ($s[$i] == $s[$j]) {
1238+
$j++;
1239+
}
1240+
$next[$i] = $j;
1241+
}
1242+
}
1243+
```
11691244
-----------------------
11701245
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0034.在排序数组中查找元素的第一个和最后一个位置.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,52 @@ var searchRange = function(nums, target) {
480480
return [-1, -1];
481481
};
482482
```
483-
483+
### Scala
484+
```scala
485+
object Solution {
486+
def searchRange(nums: Array[Int], target: Int): Array[Int] = {
487+
var left = getLeftBorder(nums, target)
488+
var right = getRightBorder(nums, target)
489+
if (left == -2 || right == -2) return Array(-1, -1)
490+
if (right - left > 1) return Array(left + 1, right - 1)
491+
Array(-1, -1)
492+
}
493+
494+
// 寻找左边界
495+
def getLeftBorder(nums: Array[Int], target: Int): Int = {
496+
var leftBorder = -2
497+
var left = 0
498+
var right = nums.length - 1
499+
while (left <= right) {
500+
var mid = left + (right - left) / 2
501+
if (nums(mid) >= target) {
502+
right = mid - 1
503+
leftBorder = right
504+
} else {
505+
left = mid + 1
506+
}
507+
}
508+
leftBorder
509+
}
510+
511+
// 寻找右边界
512+
def getRightBorder(nums: Array[Int], target: Int): Int = {
513+
var rightBorder = -2
514+
var left = 0
515+
var right = nums.length - 1
516+
while (left <= right) {
517+
var mid = left + (right - left) / 2
518+
if (nums(mid) <= target) {
519+
left = mid + 1
520+
rightBorder = left
521+
} else {
522+
right = mid - 1
523+
}
524+
}
525+
rightBorder
526+
}
527+
}
528+
```
484529

485530
-----------------------
486531
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)