当前位置:网站首页>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;
}
}
虽然结果不太好看,但终究这题有意义了,不忘初心。
边栏推荐
- Super VRT
- Sword finger offer II 091 Paint the house
- Tiktok practice ~ search page ~ scan QR code
- Separate save file for debug symbols after strip
- Tiktok practice ~ search page ~ video details
- Three basic backup methods of mongodb
- mongoDB的三种基础备份方法
- Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
- Jz-062- the k-th node of binary search tree
- Detailed explanation of retrospective thinking
猜你喜欢
随机推荐
C: 反转链表
MySQL recharge
30. concatenate substrings of all words
Unity - URP get camera stack
Database SQL statement writing
JS mobile terminal touch screen event
Tiktok practice ~ homepage video ~ pull-down refresh
0基础学c语言(1)
【贝叶斯分类3】半朴素贝叶斯分类器
Feitian +cipu body brings more imagination to the metauniverse
Minimum spanning tree, shortest path, topology sorting, critical path
JWT操作工具类分享
MySQL - subquery usage
清华大学就光刻机发声,ASML立马加紧向中国出口光刻机
[most detailed] latest and complete redis interview (42 tracks)
郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
uni-app使用canvas绘制二维码
Unit test of boot
Boot indicator monitoring