当前位置:网站首页>Sword finger offer 58 - I. flip word order
Sword finger offer 58 - I. flip word order
2022-07-27 07:50:00 【ThE wAlkIng D】
Title Description
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”.

Problem analysis
This problem uses the method of double pointers to traverse
s.trim() Method remove the space bar , Give Way i,j Traverse from the tail when i When traversing the space , Use substring Intercept , Cut out the words and put them in res newly build StringBuilder Back , End use tostring Convert to string .
Code instance
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public String reverseWords(String s) {
public String reverseWords(String s) {
s = s.trim();
int i = s.length() - 1;
int j = i;
StringBuilder res = new StringBuilder();
while(i >= 0){
while(i >= 0 && s.charAt(i) != ' '){
i--;
}
res.append(s.substring(i + 1,j + 1) + ' ');
while(i >= 0 && s.charAt(i) == ' '){
i--;
}
j = i;
}
return res.toString().trim();
}
}
}
边栏推荐
- 【Day42 文献精读】A Bayesian Model of Perceived Head-Centered Velocity during Smooth Pursuit Eye Movement
- IDEA中文乱码怎么办
- 杂谈:手里有竿儿,肩上有网,至于背篓里有多少鱼真的重要吗?
- Happy holidays, everyone
- Synchronized lock
- What other methods are available for MySQL index analysis besides explain
- [stonedb class] introductory lesson 1: popular science of database knowledge
- 物联网工业级UART串口转WiFi转有线网口转以太网网关WiFi模块选型
- 【Golang】golang开发微信公众号网页授权功能
- 孙子出题难,儿子监考严。老子不会做,还我上学钱
猜你喜欢

C# 事件用法案例 订阅事件+=

小程序支付管理-新版支付对接流程

JS regular expression implementation adds a comma to every three digits

企业架构驱动的数字化转型!

STM32_ Find the cause of entering hardfault_ Handler's function

【已解决】新版Pycharm(2022)连接服务器进行上传文件报错“Command rsync is not found in PATH”,无法同步文件

Promise详解

Clickhouse notes 1 | introduction, features | installation and use based on centos7 system | common data types | mergetree table engine | SQL operation

DASCTF2022.07赋能赛密码wp

An open source OA office automation system
随机推荐
Showdoc vulnerability learning - cnvd-2020-26585 (arbitrary file upload)
glGetUniformLocation,glUniform4f
SQL labs SQL injection platform - level 1 less-1 get - error based - Single Quotes - string (get single quote character injection based on errors)
kalibr标定realsenseD435i --多相机标定
A priority SQL problem
Resttemplate connection pool configuration
物联网工业级UART串口转WiFi转有线网口转以太网网关WiFi模块选型
这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?
电子量产项目框架--基本思想
Clickhouse notes 1 | introduction, features | installation and use based on centos7 system | common data types | mergetree table engine | SQL operation
[QT] unable to open the containing file pcap.h (C1083) in QT creator
Promise详解
综合案例、
C# 事件用法案例 订阅事件+=
OpenGL shader learning notes: varying variables
yhb_ sysbench
Jjwt generate token
LeetCode56. 合并区间
Convert objects to key value pairs
鲁迅:我不记得说没说过,要不你自己查!