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 af239ce

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents f5f5f5a + e4d638f commit af239ce

File tree

79 files changed

+3298
-140
lines changed

Some content is hidden

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

79 files changed

+3298
-140
lines changed

‎problems/0001.两数之和.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,48 @@ class Solution {
275275
}
276276
```
277277

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+
301+
C#:
302+
```csharp
303+
public class Solution {
304+
public int[] TwoSum(int[] nums, int target) {
305+
Dictionary<int ,int> dic= new Dictionary<int,int>();
306+
for(int i=0;i<nums.Length;i++){
307+
int imp= target-nums[i];
308+
if(dic.ContainsKey(imp)&&dic[imp]!=i){
309+
return new int[]{i, dic[imp]};
310+
}
311+
if(!dic.ContainsKey(nums[i])){
312+
dic.Add(nums[i],i);
313+
}
314+
}
315+
return new int[]{0, 0};
316+
}
317+
}
318+
```
319+
320+
278321
-----------------------
279322
<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/0019.删除链表的倒数第N个节点.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
分为如下几步:
4141

42-
* 首先这里我推荐大家使用虚拟头结点,这样方面处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)
42+
* 首先这里我推荐大家使用虚拟头结点,这样方便处理删除实际头结点的逻辑,如果虚拟头结点不清楚,可以看这篇: [链表:听说用虚拟头节点会方便很多?](https://programmercarl.com/0203.移除链表元素.html)
4343

4444
* 定义fast指针和slow指针,初始值为虚拟头结点,如图:
4545

@@ -289,6 +289,28 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
289289
return dummyHead.next
290290
}
291291
```
292-
292+
Scala:
293+
```scala
294+
object Solution {
295+
def removeNthFromEnd(head: ListNode, n: Int): ListNode = {
296+
val dummy = new ListNode(-1, head) // 定义虚拟头节点
297+
var fast = head // 快指针从头开始走
298+
var slow = dummy // 慢指针从虚拟头开始头
299+
// 因为参数 n 是不可变量,所以不能使用 while(n>0){n-=1}的方式
300+
for (i <- 0 until n) {
301+
fast = fast.next
302+
}
303+
// 快指针和满指针一起走,直到fast走到null
304+
while (fast != null) {
305+
slow = slow.next
306+
fast = fast.next
307+
}
308+
// 删除slow的下一个节点
309+
slow.next = slow.next.next
310+
// 返回虚拟头节点的下一个
311+
dummy.next
312+
}
313+
}
314+
```
293315
-----------------------
294316
<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/0024.两两交换链表中的节点.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,29 @@ func swapPairs(_ head: ListNode?) -> ListNode? {
311311
return dummyHead.next
312312
}
313313
```
314-
314+
Scala:
315+
```scala
316+
// 虚拟头节点
317+
object Solution {
318+
def swapPairs(head: ListNode): ListNode = {
319+
var dummy = new ListNode(0, head) // 虚拟头节点
320+
var pre = dummy
321+
var cur = head
322+
// 当pre的下一个和下下个都不为空,才进行两两转换
323+
while (pre.next != null && pre.next.next != null) {
324+
var tmp: ListNode = cur.next.next // 缓存下一次要进行转换的第一个节点
325+
pre.next = cur.next // 步骤一
326+
cur.next.next = cur // 步骤二
327+
cur.next = tmp // 步骤三
328+
// 下面是准备下一轮的交换
329+
pre = cur
330+
cur = tmp
331+
}
332+
// 最终返回dummy虚拟头节点的下一个,return可以省略
333+
dummy.next
334+
}
335+
}
336+
```
315337

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

‎problems/0027.移除元素.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public:
9393

9494
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
9595

96-
后序都会一一介绍到,本题代码如下:
96+
后续都会一一介绍到,本题代码如下:
9797

9898
```CPP
9999
// 时间复杂度:O(n)
@@ -338,6 +338,20 @@ int removeElement(int* nums, int numsSize, int val){
338338
return slow;
339339
}
340340
```
341-
341+
Scala:
342+
```scala
343+
object Solution {
344+
def removeElement(nums: Array[Int], `val`: Int): Int = {
345+
var slow = 0
346+
for (fast <- 0 until nums.length) {
347+
if (`val` != nums(fast)) {
348+
nums(slow) = nums(fast)
349+
slow += 1
350+
}
351+
}
352+
slow
353+
}
354+
}
355+
```
342356
-----------------------
343357
<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>

0 commit comments

Comments
(0)

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