当前位置:网站首页>leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
2022-06-26 20:30:00 【涛涛英语学不进去】
题目:剑指Offer 05.替换空格
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入:s = “We are happy.”
输出:“We%20are%20happy.”
想着蛮简单的,只要知道空格的Ascll码就行了。一发入魂。
没啥好说的。
package com.programmercarl.string;
/** * @ClassName ReplaceSpace * @Descriotion TODO * @Author nitaotao * @Date 2022/6/23 19:49 * @Version 1.0 * https://leetcode.cn/problems/ti-huan-kong-ge-lcof/ * 剑指 Offer 05. 替换空格 **/
public class ReplaceSpace {
public static void main(String[] args) {
// char space = ' ';
//32
// System.out.println(space - 0);
System.out.println(replaceSpace("We are happy."));
}
public static String replaceSpace(String s) {
char[] chars = s.toCharArray();
StringBuffer result = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 32) {
result.append("%20");
} else {
result.append(chars[i]);
}
}
return String.valueOf(result);
}
}

边栏推荐
- 浏览器的垃圾回收机制
- 网上办理中金财富开户安全吗?
- 710. random numbers in the blacklist
- 云计算技术的发展与芯片处理器的关系
- Is it safe to open a securities account? Is there any danger
- find_ path、find_ Library memo
- 0基础c语言(0)
- Is there any risk in opening a mobile stock registration account? Is it safe?
- Unit test of boot
- Development of NFT for digital collection platform
猜你喜欢
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered

【山东大学】考研初试复试资料分享

Super VRT

Disruptor local thread queue_ Use transprocessor processor and workpool to compare consumption - Notes on inter thread communication 005

好物推荐:移动端开发安全工具

阿里云个人镜像仓库日常基本使用

云计算技术的发展与芯片处理器的关系

How to install mysql8.0 database under Windows system? (Graphic tutorial)

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

Gee: calculate the maximum and minimum values of pixels in the image area
随机推荐
QT两种方法实现定时器
C exercise. Class list plus records, display records and clear records
Détails de l'annotation des ressources sentinelles
515. find the maximum value in each tree row
动态规划111
C primer plus learning notes - 3. Character IO (input / output)
[recommended collection] these 8 common missing value filling skills must be mastered
Gee: calculate the maximum and minimum values of pixels in the image area
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
Tiktok practice ~ sharing module ~ short video download (save to photo album)
swagger:如何生成漂亮的静态文档说明页
手机股票注册开户有没有什么风险?安全吗?
Some cold knowledge about QT database development
0 basic C language (2)
C: 反转链表
BOM and DOM operations
【贝叶斯分类2】朴素贝叶斯分类器
Daily basic use of alicloud personal image warehouse
抖音实战~搜索页面~扫描二维码
好物推薦:移動端開發安全工具