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 4 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
16 changes: 15 additions & 1 deletion problems/0344.反转字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ public class Solution
}
}
```

Scala:
```scala
object Solution {
def reverseString(s: Array[Char]): Unit = {
var (left, right) = (0, s.length - 1)
while (left < right) {
var tmp = s(left)
s(left) = s(right)
s(right) = tmp
left += 1
right -= 1
}
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
43 changes: 42 additions & 1 deletion problems/0541.反转字符串II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,47 @@ public class Solution
}
}
```

Scala:

版本一: (正常解法)
```scala
object Solution {
def reverseStr(s: String, k: Int): String = {
val res = s.toCharArray // 转换为Array好处理
for (i <- s.indices by 2 * k) {
// 如果i+k大于了res的长度,则需要全部翻转
if (i + k > res.length) {
reverse(res, i, s.length - 1)
} else {
reverse(res, i, i + k - 1)
}
}
new String(res)
}
// 翻转字符串,从start到end
def reverse(s: Array[Char], start: Int, end: Int): Unit = {
var (left, right) = (start, end)
while (left < right) {
var tmp = s(left)
s(left) = s(right)
s(right) = tmp
left += 1
right -= 1
}
}
}
```
版本二: 首先利用sliding每隔k个进行分割,随后转换为数组,再使用zipWithIndex添加每个数组的索引,紧接着利用map做变换,如果索引%2==0则说明需要翻转,否则原封不动,最后再转换为String
```scala
object Solution {
def reverseStr(s: String, k: Int): String = {
s.sliding(k, k)
.toArray
.zipWithIndex
.map(v => if (v._2 % 2 == 0) v._1.reverse else v._1)
.mkString
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
56 changes: 55 additions & 1 deletion problems/剑指Offer05.替换空格.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,62 @@ func replaceSpace(_ s: String) -> String {
}
```

Scala:


方式一: 双指针
```scala
object Solution {
def replaceSpace(s: String): String = {
var count = 0
s.foreach(c => if (c == ' ') count += 1) // 统计空格的数量
val sOldSize = s.length // 旧数组字符串长度
val sNewSize = s.length + count * 2 // 新数组字符串长度
val res = new Array[Char](sNewSize) // 新数组
var index = sNewSize - 1 // 新数组索引
// 逆序遍历
for (i <- (0 until sOldSize).reverse) {
if (s(i) == ' ') {
res(index) = '0'
index -= 1
res(index) = '2'
index -= 1
res(index) = '%'
} else {
res(index) = s(i)
}
index -= 1
}
res.mkString
}
}
```
方式二: 使用一个集合,遇到空格就添加%20
```scala
object Solution {
import scala.collection.mutable.ListBuffer
def replaceSpace(s: String): String = {
val res: ListBuffer[Char] = ListBuffer[Char]()
for (i <- s.indices) {
if (s(i) == ' ') {
res += '%'
res += '2'
res += '0'
}else{
res += s(i)
}
}
res.mkString
}
}
```
方式三: 使用map
```scala
object Solution {
def replaceSpace(s: String): String = {
s.map(c => if(c == ' ') "%20" else c).mkString
}
}
```


-----------------------
Expand Down

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