当前位置:网站首页>[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"));
}
}
边栏推荐
- Guess riddles (4)
- Typical low code apaas manufacturer cases
- 2022.7.4-----leetcode.1200
- Arrangement of some library files
- Several problems to be considered and solved in the design of multi tenant architecture
- Example 006: Fibonacci series
- Count the number of inputs (C language)
- EA introduction notes
- Cinq détails de conception du régulateur de tension linéaire
- 696. 计数二进制子串
猜你喜欢
随机推荐
Example 007: copy data from one list to another list.
2020-05-21
Guess riddles (5)
Briefly talk about the identification protocol of mobile port -bc1.2
One question per day - replace spaces
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
Explore the authentication mechanism of StarUML
TypeScript手把手教程,简单易懂
Guess riddles (7)
猜谜语啦(8)
L298N module use
每日一题——输入一个日期,输出它是该年的第几天
Numpy 小坑:维度 (n, 1) 和 维度 (n, ) 数组相加运算后维度变为 (n, n)
EA introduction notes
Several problems to be considered and solved in the design of multi tenant architecture
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
C language data type replacement
实例009:暂停一秒输出
Chapter 18 using work queue manager (1)
Meizu Bluetooth remote control temperature and humidity access homeassistant