当前位置:网站首页>[leetcode sword finger offer 58 - I. flip word order (simple)]
[leetcode sword finger offer 58 - I. flip word order (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
Enter an English sentence , Turn over the order of the words in the sentence , But the order of the characters in the word is the same . For the sake of simplicity , Punctuation is treated like ordinary letters . For example, input string "I am a student. “, The output "student. a am I”.
Example 1:
Input : “the sky is blue”
Output : “blue is sky the”
Example 2:
Input : " hello world! "
Output : “world! hello”
explain : The input string can contain extra spaces before or after , But the reversed characters cannot include .
Example 3:
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 .
explain :
No space characters make up a word .
The input string can contain extra spaces before or after , But the reversed characters cannot include .
If there are extra spaces between two words , Reduce the space between inverted words to just one .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
class Solution {
public String reverseWords(String s) {
// trim() Method is used to delete the leading and trailing whitespace of a string ,split() Method splits a string into spaces
String[] strings = s.trim().split(" ");
StringBuilder sb = new StringBuilder();
for(int i = strings.length-1;i>=0;i--){
// Prevent consecutive spaces
if (strings[i].equals("")) {
continue;
}
if(i>0)
sb.append(strings[i] + " ");
else
sb.append(strings[i]);
}
return sb.toString();
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479016.html
边栏推荐
- Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
- Ubuntu 20 installing centernet
- Centralized record of experimental problems
- 电脑越用越慢怎么办?文件误删除恢复方法
- Norm normalization in tensorflow and pytorch of records
- ubutnu20安裝CenterNet
- PHP的Laravel与Composer部署项目时常见问题
- 韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
- 【SDU Chart Team - Core】SVG属性类设计之枚举
- Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
猜你喜欢
Linux中,MySQL的常用命令
1.整理华子面经--1
一文解决浏览器跨域问题
[leetcode refers to offer 27. Image of binary tree (simple)]
2.整理华子面经--2
Question brushing plan - depth first search (II)
On the three paradigms of database design
How to make Jenkins job run automatically after startup
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Google 尝试在 Chrome 中使用 Rust
随机推荐
Deep understanding of modern mobile GPU (continuously updating)
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
Introduce structured concurrency and release swift 5.5!
Addition, deletion, modification and query of MySQL advanced table
Factory mode
使用mbean 自动执行heap dump
亚马逊和Epic将入驻,微软应用商城向第三方开放
Sequential state
How to learn software testing? Self study or training? After reading this article, you will understand
Reference of custom message in ROS function pack failed
学会打字后的思考
Thinking after learning to type
Prim、Kruskal
41. 缺失的第一个正数
Presto on spark supports 3.1.3 records
[SDU chart team - core] enumeration of SVG attribute class design
笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
Recommended usage scenarios and production tools for common 60 types of charts
Gsi-ecm digital platform for engineering construction management