mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
更新 剑指Offer05.替换空格 排版格式修复
This commit is contained in:
@ -15,7 +15,7 @@
|
|||||||
输入:s = "We are happy."
|
输入:s = "We are happy."
|
||||||
输出:"We%20are%20happy."
|
输出:"We%20are%20happy."
|
||||||
|
|
||||||
# 思路
|
## 思路
|
||||||
|
|
||||||
如果想把这道题目做到极致,就不要只用额外的辅助空间了!
|
如果想把这道题目做到极致,就不要只用额外的辅助空间了!
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ public:
|
|||||||
* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html)
|
* [142.环形链表II](https://programmercarl.com/0142.环形链表II.html)
|
||||||
* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html)
|
* [344.反转字符串](https://programmercarl.com/0344.反转字符串.html)
|
||||||
|
|
||||||
# 拓展
|
## 拓展
|
||||||
|
|
||||||
这里也给大家拓展一下字符串和数组有什么差别,
|
这里也给大家拓展一下字符串和数组有什么差别,
|
||||||
|
|
||||||
@ -121,7 +121,8 @@ for (int i = 0; i < a.size(); i++) {
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
C:
|
### C:
|
||||||
|
|
||||||
```C
|
```C
|
||||||
char* replaceSpace(char* s){
|
char* replaceSpace(char* s){
|
||||||
//统计空格数量
|
//统计空格数量
|
||||||
@ -152,8 +153,8 @@ char* replaceSpace(char* s){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Java:
|
||||||
|
|
||||||
Java:
|
|
||||||
```Java
|
```Java
|
||||||
//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制
|
//使用一个新的对象,复制 str,复制的过程对其判断,是空格则替换,否则直接复制,类似于数组复制
|
||||||
public static String replaceSpace(String s) {
|
public static String replaceSpace(String s) {
|
||||||
@ -211,8 +212,8 @@ public String replaceSpace(String s) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Go:
|
||||||
|
|
||||||
Go:
|
|
||||||
```go
|
```go
|
||||||
// 遍历添加
|
// 遍历添加
|
||||||
func replaceSpace(s string) string {
|
func replaceSpace(s string) string {
|
||||||
@ -264,9 +265,10 @@ func replaceSpace(s string) string {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### python:
|
||||||
|
|
||||||
|
因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
|
||||||
|
|
||||||
python:
|
|
||||||
#### 因为字符串是不可变类型,所以操作字符串需要将其转换为列表,因此空间复杂度不可能为O(1)
|
|
||||||
(版本一)转换成列表,并且添加相匹配的空间,然后进行填充
|
(版本一)转换成列表,并且添加相匹配的空间,然后进行填充
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -328,7 +330,7 @@ class Solution:
|
|||||||
def replaceSpace(self, s: str) -> str:
|
def replaceSpace(self, s: str) -> str:
|
||||||
return s.replace(' ', '%20')
|
return s.replace(' ', '%20')
|
||||||
```
|
```
|
||||||
javaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
@ -366,7 +368,7 @@ javaScript:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function replaceSpace(s: string): string {
|
function replaceSpace(s: string): string {
|
||||||
@ -393,7 +395,7 @@ function replaceSpace(s: string): string {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func replaceSpace(_ s: String) -> String {
|
func replaceSpace(_ s: String) -> String {
|
||||||
@ -434,7 +436,7 @@ func replaceSpace(_ s: String) -> String {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Scala:
|
### Scala:
|
||||||
|
|
||||||
方式一: 双指针
|
方式一: 双指针
|
||||||
```scala
|
```scala
|
||||||
@ -491,8 +493,8 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### PHP:
|
||||||
|
|
||||||
PHP:
|
|
||||||
```php
|
```php
|
||||||
function replaceSpace($s){
|
function replaceSpace($s){
|
||||||
$sLen = strlen($s);
|
$sLen = strlen($s);
|
||||||
@ -527,7 +529,7 @@ function spaceLen($s){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust
|
### Rust:
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -563,3 +565,4 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user