当前位置:网站首页>[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
[formation quotidienne - Tencent Selection 50] 557. Inverser le mot III dans la chaîne
2022-07-05 08:37:00 【Puppet】
Titre
Donner une chaîne s ,Vous devez inverser l'ordre des caractères de chaque mot dans la chaîne,Tout en conservant l'ordre initial des espaces et des mots.
Exemple 1:
Entrée:s = “Let’s take LeetCode contest”
Produits:“s’teL ekat edoCteeL tsetnoc”
Exemple 2:
Entrée: s = “God Ding”
Produits:“doG gniD”
Conseils:
1 <= s.length <= 5 * 104
s Contient des ASCII Les caractères.
s Ne contient aucun espace de début ou de fin.
s - Oui. Au moins Il y a un mot..
s Tous les mots sont séparés par un espace
Code
package tencent50;
public class leetcode557 {
// Utilisation d'espace supplémentaire,Et découper en segments par des espaces dans la chaîne originale,Retourner chaque segment
public String reverseWords(String s) {
StringBuffer sb = new StringBuffer();
int len = s.length();
int i = 0;
while( i < len){
int start = i;
// Trouvez d'abord où est l'espace
while (i < len && s.charAt(i) != ' '){
i++;
}
// Retourner le mot
for (int j = i - 1; j >= start; j--){
sb.append(s.charAt(j));
}
// Espace après saut
while (i < len && s.charAt(i) == ' '){
i++;
sb.append(' ');
}
}
return sb.toString();
}
public static void main(String[] args) {
leetcode557 obj = new leetcode557();
System.out.println(obj.reverseWords("Let's take LeetCode contest"));
}
}
边栏推荐
- On boost circuit
- 287. Looking for repeats - fast and slow pointer
- Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase
- STM32---IIC
- Sword finger offer 09 Implementing queues with two stacks
- 2022.7.4-----leetcode. one thousand and two hundred
- STM32 single chip microcomputer -- volatile keyword
- 猜谜语啦(9)
- 剑指 Offer 06. 从尾到头打印链表
- STM32 single chip microcomputer - external interrupt
猜你喜欢

leetcode - 445. 两数相加 II

Sword finger offer 09 Implementing queues with two stacks

319. Bulb switch

MATLAB小技巧(28)模糊综合评价

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

猜谜语啦(8)

Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)

Sword finger offer 05 Replace spaces

EA introduction notes

剑指 Offer 09. 用两个栈实现队列
随机推荐
Is the security account given by Yixue school safe? Where can I open an account
Low code platform | apaas platform construction analysis
Business modeling of software model | vision
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
Sword finger offer 09 Implementing queues with two stacks
U8g2 drawing
STM32 single chip microcomputer -- volatile keyword
Business modeling of software model | object modeling
Cmder of win artifact
Go dependency injection -- Google open source library wire
Old Wang's esp8266 and old Wu's ws2818 light strip
猜谜语啦(10)
Example 007: copy data from one list to another list.
go依赖注入--google开源库wire
Buildroot system for making raspberry pie cm3
STM32 --- configuration of external interrupt
Guess riddles (142)
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
C language data type replacement
Some pitfalls of win10 network sharing