当前位置:网站首页>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;
}
}

虽然结果不太好看,但终究这题有意义了,不忘初心。
边栏推荐
- Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
- 460million zongzi were sold in half a year. How big is the "imagination space" of time-honored brands?
- 浏览器事件循环
- 网上开户万一免五到底安不安全?
- Is it safe to open an online account in case of five-year exemption?
- Detailed explanation of retrospective thinking
- Summary of several common UML diagrams
- Kubernetes 资源拓扑感知调度优化
- 30. 串联所有单词的子串
- C language 99 multiplication table
猜你喜欢

Tiktok practice ~ search page ~ video details

How to install mysql8.0 database under Windows system? (Graphic tutorial)

On the escape of inequality value

Tiktok practice ~ homepage video ~ pull-down refresh

数据库SQL语句撰写

Kubernetes 资源拓扑感知调度优化
![[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)](/img/a8/622cddae2ac8c383979ed51d36bca9.jpg)
[serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)

Minimum spanning tree, shortest path, topology sorting, critical path

Detailed explanation of shutter textfield

Flutter TextField详解
随机推荐
0 basic C language (3)
Tiktok practice ~ homepage video ~ pull-down refresh
股票开户的具体步骤是什么?网上开户安全吗?
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
0基础学c语言(1)
Dynamic planning 111
Selection of database paradigm and main code
Kubernetes resource topology aware scheduling optimization
动态规划111
Tiktok practice ~ sharing module ~ copy short video link
Kubernetes 资源拓扑感知调度优化
Idea error: process terminated
C language file cursor fseek
0基础学c语言(3)
C language simple login
uni-app使用canvas绘制二维码
Boot指标监测
Fixed length memory pool
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
