当前位置:网站首页>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
边栏推荐
猜你喜欢

多线程与高并发(三)—— 源码解析 AQS 原理

MySQL架构原理

【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03

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

Illustrate three mainstream enterprise architecture models (recommended collection!)

5. Dynamic programming -- Fibonacci series

记录一次idea中的父子项目修改project与module名称,亲测!

【微信小程序】项目实战—抽签应用

6. Double pointer -- the sum of the two numbers of the incremental array is equal to the target number

Flink - checkpoint Failure reason: Not all required tasks are currently running
随机推荐
Skillfully use NGX_ Lua makes traffic grouping
漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
Performance test of API gateway APIs IX in Google cloud T2a and T2D
LinkedList source massage, ah comfortable
SuperMap iServer发布管理以及调用地图服务
7、二分法——寻找一组重复或者有序但是旋转的数组
简介
9. Delete nodes in the linked list
centos7下安装mysql,网上文章都不太准
Etcd(高可用KV数据库)
13 probability distributions that must be understood in deep learning
初识SuperMap iDesktop
Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!
select如果不加order by子句,返回结果的顺序是不可靠的
[openharmony] [rk2206] build openharmony compiler (2)
Vulnerability analysis hevd-0x8.integeroverflow[win7x86]
Go json.Decoder Considered Harmful
【云驻共创】华为云:MetaStudio数字内容生产线,让虚拟世界与现实世界无缝融合
14、双指针——盛最多水的容器
️雄关漫道真如铁,而今迈步从头越️