当前位置:网站首页>Sword finger offer notes: t58 - I. flip word order
Sword finger offer notes: t58 - I. flip word order
2022-07-27 11:48:00 【Ignorant little nine】
T58 - I. Flip word order
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 .
Ideas
Double pointer , It's just the opposite
solution 1
I didn't think of it
solution 2
class Solution {
public String reverseWords(String s) {
String tmp = s.trim();
int start = tmp.length() - 1;
int end = tmp.length() - 1;
String res = "";
while(start >= 0) {
while(start >= 0 && tmp.charAt(start) != ' ') {
start--;
}
res += tmp.substring(start + 1, end + 1) + " ";
while(start >= 0 && tmp.charAt(start) == ' ') {
start--;
}
end = start;
}
return res.trim();
}
}
Execution time :14 ms, In all Java Defeated in submission **9.02%** Users of
Memory consumption :38.9 MB, In all Java Defeated in submission **10.76%** Users of
author : Painter Dapeng
link :https://leetcode-cn.com/leetbook/read/illustrate-lcof/59otf1/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- Open source Flink has datastream connector written with holo or Flink SQL Conn
- 剑指 Offer 笔记: T39. 数组中出现次数超过一半的数字
- Could not load dynamic library ‘libcudnn.so.8‘;
- Some commonly used shortcut keys for MathType
- Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
- Stm32f10x -- C Language-1
- Sword finger offer note: T39. Numbers that appear more than half of the time in the array
- 为什么TCP三次握手的时候ACK=Seq+1
- 本地虚拟机初始化脚本
- JS字符串方法总结
猜你喜欢

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
![[machine learning whiteboard derivation series] learning notes --- conditional random fields](/img/cc/87d8822b659b31360546adf057ef00.png)
[machine learning whiteboard derivation series] learning notes --- conditional random fields

How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)

【机器学习-白板推导系列】学习笔记---条件随机场

为什么TCP三次握手的时候ACK=Seq+1

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

LNMP架构搭建(部署Discuz论坛)

基于反馈率的控制系统原理

PWM的原理和PWM波的产生
随机推荐
Stm32f10x -- C Language-1
美现首例孕妇猴痘病例:新生儿被注射免疫球蛋白,已安全出生
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
微机和单片机的区别
Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
The C programming language 2nd -- Notes -- 6.7
Maker Hongmeng application development training 04
TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值
Maker Hongmeng application development training notes 03
w.r.t. ; i.e.; etc.; e. G. what does it mean
EfficientNet
源码编译安装LAMP
Codeforces round #664C
zabbix自定义监控项
检定和校准的区别
剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
Could not load dynamic library ‘libcudnn.so.8‘;
Source code compilation and installation lamp
LAN SDN technology hard core insider 12 cloud CP's daily love - hardware vxlan forwarding plane
The C programming language (2nd) -- Notes -- 1.7