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

[pull] master from youngyangyang04:master #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 18 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
abe0023
添加 0001.两数之和.md Scala版本
wzqwtt May 13, 2022
53379c0
添加 0454.四数相加II.md Scala版本
wzqwtt May 13, 2022
16c6abf
添加 0383.赎金信.md Scala版本
wzqwtt May 13, 2022
ca27111
添加(0714.买卖股票的最佳时机含手续费动态规划.md):增加typescript版本
xiaofei-2020 May 13, 2022
a53da7b
添加 0015.三数之和.md Scala版本
wzqwtt May 14, 2022
68eed4a
添加 0018.四数之和.md Scala版本
wzqwtt May 14, 2022
e4da60a
添加 0344.反转字符串.md Scala版本
wzqwtt May 14, 2022
f2dcdbe
添加 0541.反转字符串II.md Scala版本
wzqwtt May 14, 2022
037bebb
添加 剑指Offer05.替换空格.md Scala版本
wzqwtt May 14, 2022
1c369bb
102 in rust
3Xpl0it3r May 14, 2022
b8b62ff
树深度 rust实现
3Xpl0it3r May 18, 2022
9611896
Merge branch 'youngyangyang04:master' into master
3Xpl0it3r May 18, 2022
8c2737d
Merge branch 'master' into patch08
youngyangyang04 Jun 4, 2022
71a9111
Merge pull request #1326 from wzqwtt/patch08
youngyangyang04 Jun 4, 2022
b91d3c2
Merge pull request #1327 from xiaofei-2020/dp39
youngyangyang04 Jun 4, 2022
83726ac
Merge pull request #1328 from wzqwtt/patch09
youngyangyang04 Jun 4, 2022
dab44f5
Merge pull request #1329 from wzqwtt/patch10
youngyangyang04 Jun 4, 2022
9c32528
Merge pull request #1330 from 3Xpl0it3r/master
youngyangyang04 Jun 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions problems/0001.两数之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@ class Solution {
}
}
```

Scala:
```scala
object Solution {
// 导入包
import scala.collection.mutable
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
// key存储值,value存储下标
val map = new mutable.HashMap[Int, Int]()
for (i <- nums.indices) {
val tmp = target - nums(i) // 计算差值
// 如果这个差值存在于map,则说明找到了结果
if (map.contains(tmp)) {
return Array(map.get(tmp).get, i)
}
// 如果不包含把当前值与其下标放到map
map.put(nums(i), i)
}
// 如果没有找到直接返回一个空的数组,return关键字可以省略
new Array[Int](2)
}
}
```

C#:
```csharp
public class Solution {
Expand All @@ -293,5 +317,6 @@ public class Solution {
}
```


-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
65 changes: 65 additions & 0 deletions problems/0383.赎金信.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,70 @@ impl Solution {
}
```

Scala:

版本一: 使用数组作为哈希表
```scala
object Solution {
def canConstruct(ransomNote: String, magazine: String): Boolean = {
// 如果magazine的长度小于ransomNote的长度,必然是false
if (magazine.length < ransomNote.length) {
return false
}
// 定义一个数组,存储magazine字符出现的次数
val map: Array[Int] = new Array[Int](26)
// 遍历magazine字符串,对应的字符+=1
for (i <- magazine.indices) {
map(magazine(i) - 'a') += 1
}
// 遍历ransomNote
for (i <- ransomNote.indices) {
if (map(ransomNote(i) - 'a') > 0)
map(ransomNote(i) - 'a') -= 1
else return false
}
// 如果上面没有返回false,直接返回true,关键字return可以省略
true
}
}
```

```scala
object Solution {
import scala.collection.mutable
def canConstruct(ransomNote: String, magazine: String): Boolean = {
// 如果magazine的长度小于ransomNote的长度,必然是false
if (magazine.length < ransomNote.length) {
return false
}
// 定义map,key是字符,value是字符出现的次数
val map = new mutable.HashMap[Char, Int]()
// 遍历magazine,把所有的字符都记录到map里面
for (i <- magazine.indices) {
val tmpChar = magazine(i)
// 如果map包含该字符,那么对应的value++,否则添加该字符
if (map.contains(tmpChar)) {
map.put(tmpChar, map.get(tmpChar).get + 1)
} else {
map.put(tmpChar, 1)
}
}
// 遍历ransomNote
for (i <- ransomNote.indices) {
val tmpChar = ransomNote(i)
// 如果map包含并且该字符的value大于0,则匹配成功,map对应的--,否则直接返回false
if (map.contains(tmpChar) && map.get(tmpChar).get > 0) {
map.put(tmpChar, map.get(tmpChar).get - 1)
} else {
return false
}
}
// 如果上面没有返回false,直接返回true,关键字return可以省略
true
}
}


C#:
```csharp
public bool CanConstruct(string ransomNote, string magazine) {
Expand All @@ -379,6 +443,7 @@ public bool CanConstruct(string ransomNote, string magazine) {
}
return true;
}

```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
37 changes: 37 additions & 0 deletions problems/0454.四数相加II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,43 @@ impl Solution {
}
```


Scala:
```scala
object Solution {
// 导包
import scala.collection.mutable
def fourSumCount(nums1: Array[Int], nums2: Array[Int], nums3: Array[Int], nums4: Array[Int]): Int = {
// 定义一个HashMap,key存储值,value存储该值出现的次数
val map = new mutable.HashMap[Int, Int]()
// 遍历前两个数组,把他们所有可能的情况都记录到map
for (i <- nums1.indices) {
for (j <- nums2.indices) {
val tmp = nums1(i) + nums2(j)
// 如果包含该值,则对他的key加1,不包含则添加进去
if (map.contains(tmp)) {
map.put(tmp, map.get(tmp).get + 1)
} else {
map.put(tmp, 1)
}
}
}
var res = 0 // 结果变量
// 遍历后两个数组
for (i <- nums3.indices) {
for (j <- nums4.indices) {
val tmp = -(nums3(i) + nums4(j))
// 如果map中存在该值,结果就+=value
if (map.contains(tmp)) {
res += map.get(tmp).get
}
}
}
// 返回最终结果,可以省略关键字return
res
}
}

C#:
```csharp
public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Expand Down

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