当前位置:网站首页>[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
边栏推荐
- Pytorch selects the first k maximum (minimum) values and their indexes in the data
- Common commands of MySQL in Linux
- Question brushing plan -- backtracking method (I)
- UKFslam
- Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
- [leetcode refers to offer 27. Image of binary tree (simple)]
- C# 知识
- Go limit depth traversal of files in directory
- Linux中,MySQL的常用命令
- Fastdfs mind map
猜你喜欢
Two Stage Detection
Addition, deletion, modification and query of MySQL advanced table
100天拿下11K,转岗测试的超全学习指南
Google tries to use rust in Chrome
Gsi-ecm digital platform for engineering construction management
MySQL基础之写表(创建表)
Problem brushing plan -- dynamic programming (III)
Yolov5 NMS source code understanding
On the three paradigms of database design
go interface
随机推荐
3-5通过XSS获取cookie以及XSS后台管理系统的使用
如何发挥测试策略的指导性作用
Awk example skills
MySQL advanced common functions
Rust更适合经验较少的程序员?
Automatic heap dump using MBean
Crisis is opportunity. Why will the efficiency of telecommuting improve?
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
flomo软件推荐
Sequential state
Some thoughts on super in pytorch, combined with code
Alibaba cloud responded to the disclosure of user registration information
引入结构化并发,Swift 5.5 发布!
居家第二十三天的午饭
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
IOT design and development
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Is qiniu school useful and is the recommended securities account safe
一文解决浏览器跨域问题