当前位置:网站首页>leetcode刷题:字符串04(颠倒字符串中的单词)
leetcode刷题:字符串04(颠倒字符串中的单词)
2022-06-26 20:30:00 【涛涛英语学不进去】
151.翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词。
示例 1:
输入: “the sky is blue”
输出: “blue is sky the”
示例 2:
输入: " hello world! "
输出: “world! hello”
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
示例 3:
输入: “a good example”
输出: “example good a”
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
这题一拿来,我看这不很简单吗
public static String reverseWords(String s) {
StringBuilder result = new StringBuilder("");
s = s.trim();
String[] strs = s.split(" ");
for (int i = strs.length - 1; i > 0; i--) {
//防止连续的空格
if (!"".equals(strs[i])) {
result.append(strs[i]).append(" ");
}
}
//最后一位不加空格
result.append(strs[0]);
return String.valueOf(result);
}

简单的看一下题解
所以最后为了让这题有意义,我按解题思路又做了一遍
package com.programmercarl.string;
/** * @ClassName ReverseWords * @Descriotion TODO * @Author nitaotao * @Date 2022/6/25 14:30 * @Version 1.0 * https://leetcode.cn/problems/reverse-words-in-a-string/ * 151. 颠倒字符串中的单词 **/
public class ReverseWords {
public static void main(String[] args) {
System.out.println(reverseWords("the sky is blue"));
}
/** * 不使用java内置的方法 * 1 去除多于空格 * 2 反转字符串 * 3 反转单词 * * @param s * @return */
public static String reverseWords(String s) {
StringBuilder result;
//去除多余空格
result = removeSpace(s);
//反转整个字符串
result = reverseStr(result, 0, result.length());
//反转每个单词
int start = 0;
int end = 0;
for (int i = 0; i < result.length(); i++) {
if (result.charAt(end) == ' ') {
result = reverseStr(result, start, end);
end++;
start = end;
} else {
end++;
}
}
//反转最后一个单词
result = reverseStr(result, start, result.length());
return String.valueOf(result);
}
/** * 去除多余空格 * * @param s * @return */
public static StringBuilder removeSpace(String s) {
StringBuilder result = new StringBuilder("");
char[] strs = s.toCharArray();
for (int i = 0; i < strs.length; i++) {
//判断是否是首部的空格
if (strs[0] == ' ' && i == 0) {
continue;
}
//判断是否是中间多于的空格
if (strs[i] == ' ' && strs[i - 1] == ' ') {
continue;
}
result.append(strs[i]);
}
//判断最后一位是否是空格
if (result.charAt(result.length() - 1) == ' ') {
result.deleteCharAt(result.length() - 1);
}
return result;
}
/** * 反转指定区间内字符串 * * @param s * @param start * @param end * @return */
public static StringBuilder reverseStr(StringBuilder s, int start, int end) {
while (start < end) {
char temp = s.charAt(start);
s.replace(start, start + 1, String.valueOf(s.charAt(end - 1)));
s.replace(end - 1, end, String.valueOf(temp));
start++;
end--;
}
return s;
}
}

虽然结果不太好看,但终究这题有意义了,不忘初心。
边栏推荐
- Six necessary threat tracking tools for threat hunters
- MySQL - table creation and management
- Is it safe to open a securities account? Is there any danger
- 飞天+CIPU体为元宇宙带来更大想象空间
- 证券开户安全吗,有没有什么危险呢
- QT两种方法实现定时器
- 【山东大学】考研初试复试资料分享
- Idea error: process terminated
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- Unit test of boot
猜你喜欢
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered

Tiktok practice ~ sharing module ~ generate short video QR code

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印

清华大学就光刻机发声,ASML立马加紧向中国出口光刻机

Disruptor本地线程队列_使用transProcessor处理器和WorkPool两种方式进行消费对比---线程间通信工作笔记005

MySQL recharge

Boot的单元测试

Six necessary threat tracking tools for threat hunters

Tiktok practice ~ search page ~ video details
随机推荐
Gd32 USB composite device file descriptor
Some cold knowledge about QT database development
两个文件 合并为第三个文件 。
JS mobile terminal touch screen event
Tiktok practice ~ homepage video ~ pull-down refresh
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
网上开户万一免五到底安不安全?
超分之VRT
c语言99乘法表
When are global variables initialized before entering the main function?
0 basic C language (0)
股票开户的具体步骤是什么?网上开户安全吗?
【贝叶斯分类3】半朴素贝叶斯分类器
Tiktok practice ~ search page ~ scan QR code
Developer survey: rust/postgresql is the most popular, and PHP salary is low
2022/02/14 line generation
C language simple login
swagger:如何生成漂亮的静态文档说明页
Unit test of boot
c语言简单的登录
