当前位置:网站首页>16. String inversion
16. String inversion
2022-07-28 10:17:00 【[email protected]】
Title Description
- Input : " hello world! "
Output : “world! hello”
explain : The input string can contain extra spaces before or after , But the reversed characters cannot include . - Input : “a good example”
Output : “example good a”
explain : If there are extra spaces between two words , Reduce the space between inverted words to just one .
public String reverseWords(String s) {
String[] strs = s.trim().split(" ");// Delete leading and trailing spaces , Split string
StringBuilder res = new StringBuilder();
for(int i = strs.length - 1; i >= 0; i--){// Traverse the word list in reverse order
if(strs[i].equals(" ")) continue;// If you encounter an empty word, skip
res.append(strs[i] + " ");// Splice words into StringBuilder
}
return res.toString().trim();// Convert to string , Delete trailing spaces , And back to
}
General idea : Store the string in the array , Then traverse from the end of the array , But when traversing , When you encounter two spaces , At this time, we should ignore it , Continue traversing , At the same time, each character traversed is stored in StringBuilder, Finally, it is converted to string output .
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207280958223543.html
边栏推荐
- Kubernetes
- 配置树莓派,过程和遇到问题
- UEditor V1.4.3控制文件压缩
- 吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
- Description of landingsite electronic label quppa firmware entering DFU status
- 华为入股石墨烯材料厂商富烯科技,持股10%
- 印度计划禁用中国电信设备!真离得开华为、中兴?
- 【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合
- In retaliation for the dismissal of the company, I changed all code comments of the project!
- MySQL架构原理
猜你喜欢

软件设计师考前20问,注意啦!!

5. Dynamic programming -- Fibonacci series

6、双指针——递增数组两数之和与目标数相等

15、判断二维数组中是否存在目标值

Performance test of API gateway APIs IX in Google cloud T2a and T2D

B2B e-commerce website scheme for building materials industry: enable the transformation and upgrading of building materials enterprises to achieve cost reduction and efficiency improvement

Aqua Data Studio 18.5.0导出insert语句

巧用ngx_lua做流量分组

What are the highlights of B2B2C system? How to help jewelry enterprises build an omni channel multi merchant mall management system
![[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC](/img/16/e5aa43df6ef9bdbc173fa547c85559.png)
[esp32][esp idf][lvgl7.9] failed to compile with OLED IIC
随机推荐
Vulnerability analysis hevd-0x8.integeroverflow[win7x86]
brief introduction
3.用数组逆序打印链表
Context values traps and how to avoid or mitigate these traps in go
Uni app advanced creation component / native rendering
传全球半导体设备巨头或将于上海建合资工厂!
Status Notice ¶
我用小程序容器让移动研发效率提升了5倍!
Consul
6、双指针——递增数组两数之和与目标数相等
11、链表反转
5、动态规划---斐波那契数列
Go 内存模型 (2014年5月31日版本)
DBeaver的操作日志
Tencent technical experts: decrypt the 100 million user products wechat, QQ, King glory... Comprehensively practice on the cloud!
Kubernetes
指令系统超全知识点详解
数据库mysql基础
4.调整数组顺序使奇数位于偶数前面
14、双指针——盛最多水的容器