当前位置:网站首页>[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
边栏推荐
- Send email to laravel
- 2.整理华子面经--2
- MySQL advanced common functions
- South Korea may ban apple and Google from offering commission to developers, the first in the world
- 管道和xargs
- IOT 设计与开发
- airbase 初步分析
- Addition, deletion, modification and query of advanced MySQL data (DML)
- Preliminary analysis of Airbase
- On the three paradigms of database design
猜你喜欢

Yolov5 NMS source code understanding

一文解决浏览器跨域问题

Use 3080ti to run tensorflow GPU = 1 X version of the source code

浅谈数据库设计之三大范式

go interface

Mysql database common sense storage engine

Fastdfs思维导图

Addition, deletion, modification and query of advanced MySQL data (DML)

MySQL数据库常识之储存引擎

FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
随机推荐
Lunch on the 23rd day at home
41. 缺失的第一个正数
go interface
Alibaba cloud responded to the disclosure of user registration information
flomo软件推荐
go reflect
Fastdfs思维导图
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
Go limit depth traversal of files in directory
How to learn software testing? Self study or training? After reading this article, you will understand
Fastdfs mind map
Gsi-ecm digital platform for engineering construction management
Common problems in deploying projects with laravel and composer for PHP
On the three paradigms of database design
Addition, deletion, modification and query of advanced MySQL data (DML)
The computer is out of power. How did I pass the terrible interview of Tencent cloud?
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
深入探究ASP.NET Core读取Request.Body的正确方式
ros功能包内自定义消息引用失败
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal