当前位置:网站首页>[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
边栏推荐
- Ubutnu20 installer centernet
- 浅谈数据库设计之三大范式
- Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
- Problem brushing plan -- dynamic programming (III)
- YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
- 使用mbean 自动执行heap dump
- pikachuxss如何获取cookie靶场,返回首页总是失败
- Tensorflow realizes gradient accumulation, and then returns
- Deep analysis of C language function
- Win 11K in 100 days, super complete learning guide for job transfer test
猜你喜欢
Keywords static, extern + global and local variables
管道和xargs
Fastdfs思维导图
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Thinkphp5 + data large screen display effect
[leetcode refers to offer 27. Image of binary tree (simple)]
浅谈数据库设计之三大范式
随机推荐
Two Stage Detection
Yolov5 NMS source code understanding
3-5 obtaining cookies through XSS and the use of XSS background management system
Reference of custom message in ROS function pack failed
Introduce structured concurrency and release swift 5.5!
Keywords static, extern + global and local variables
引入结构化并发,Swift 5.5 发布!
pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
韩国或将禁止苹果和谷歌向开发者抽佣 创全球首例
Norm normalization in tensorflow and pytorch of records
DeNO 1.13.2 release
go struct
Rust更适合经验较少的程序员?
使用mbean 自动执行heap dump
Is qiniu school useful and is the recommended securities account safe
flomo软件推荐
2.整理华子面经--2
Factory mode
ROS learning notes - tutorial on the use of ROS