首页 > 其他分享 >【LeeCode】151. 反转字符串中的单词

【LeeCode】151. 反转字符串中的单词

时间:2023-02-18 23:33:38浏览次数:60  
标签:151 String Solution public 单词 LeeCode sb new reverseWords

【题目描述】

给你一个字符串 ​​s​​ ,请你反转字符串中 单词 的顺序。

单词 是由非空格字符组成的字符串。​​s​​ 中使用至少一个空格将字符串中的 单词 分隔开。

返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。

注意:输入字符串 ​​s​​中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。

​https://leetcode.cn/problems/reverse-words-in-a-string/​


【示例】

【LeeCode】151. 反转字符串中的单词_Test


【代码】admin

思路: 基于空格分隔(正则), 然后反向输出即可

package com.company;
import java.util.*;

// 2022-02-18
class Solution {
public String reverseWords(String s) {
String str = s.trim();
String[] split = str.split("\\s+");
StringBuilder sb = new StringBuilder();

for (int i = split.length - 1; i >= 0; i--){
sb.append(split[i]).append(" ");
}
// System.out.println(sb.toString().trim());
return sb.toString().trim();
}
}
public class Test {
public static void main(String[] args) {
new Solution().reverseWords("the sky is blue"); // 输出:"blue is sky the"
new Solution().reverseWords( " hello world "); // 输出:"world hello"
new Solution().reverseWords( "a good example"); // 输出:"example good a"
}
}


【代码】​​LeeCode--内置函数​

【LeeCode】151. 反转字符串中的单词_字符串_02

package com.company;
import java.util.*;

// 2022-02-18
class Solution {
public String reverseWords(String s) {
s = s.trim();
List<String> strings = Arrays.asList(s.split("\\s+"));
Collections.reverse(strings);
System.out.println(String.join(" ", strings));
return String.join(" ", strings);
}
}
public class Test {
public static void main(String[] args) {
new Solution().reverseWords("the sky is blue"); // 输出:"blue is sky the"
new Solution().reverseWords( " hello world "); // 输出:"world hello"
new Solution().reverseWords( "a good example"); // 输出:"example good a"
}
}


【代码】​​LeeCode--双端队列​

【LeeCode】151. 反转字符串中的单词_字符串_03

package com.company;
import java.util.*;

// 2022-02-18
class Solution {
public String reverseWords(String s) {

int left = 0;
int right = s.length() - 1;
// 去掉开头字符
while (left <= right && s.charAt(left) == ' '){
++left;
}
// 去掉结尾字符
while (left <= right && s.charAt(right) == ' '){
--right;
}

Deque<String> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
while (left < right){
char c = s.charAt(left);
if ((sb.length() != 0) && (c == ' ')){
stack.offerFirst(sb.toString());
sb.setLength(0);
}else if (c != ' '){
sb.append(c);
}
left++;
}
stack.offerLast(sb.toString());
return String.join(" ", stack);
}
}
public class Test {
public static void main(String[] args) {
new Solution().reverseWords("the sky is blue"); // 输出:"blue is sky the"
new Solution().reverseWords( " hello world "); // 输出:"world hello"
new Solution().reverseWords( "a good example"); // 输出:"example good a"
}
}

标签:151,String,Solution,public,单词,LeeCode,sb,new,reverseWords
From: https://blog.51cto.com/u_13682316/6065705

相关文章

  • 【LeeCode】344. 反转字符串
    【题目描述】编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 ​​s​​ 的形式给出。不要给另外的数组分配额外的空间,你必须​​原地​​修改输入数......
  • 1515:网络协议
     板子题缩点找出入度为0的点即可第二问max(p,q) #include<bits/stdc++.h>usingnamespacestd;constintN=103;vector<int>g[N],graph[N];intn,pool......
  • 【LeeCode】135. 分发糖果 -- todo
    【题目描述】​​n​​​ 个孩子站成一排。给你一个整数数组 ​​ratings​​ 表示每个孩子的评分。你需要按照以下要求,给这些孩子分发糖果:每个孩子至少分配到 ​​1​......
  • 【LeeCode】二叉树理论
    二叉树分类没有数值满⼆叉树如果⼀棵⼆叉树只有度为0的结点和度为2的结点,并且度为0的结点在同⼀层上,则这棵⼆叉树为满⼆叉树​满⼆叉树,也可以说深度为k,有2^k-1个节点的⼆叉......
  • 【LeeCode】406. 根据身高重建队列
    【题目描述】假设有打乱顺序的一群人站成一个队列,数组 ​​people​​​ 表示队列中一些人的属性(不一定按顺序)。每个 ​​people[i]=[hi,ki]​​​ 表示第 ​​i​......
  • 【算法训练营day46】LeetCode139. 单词拆分 多重背包基础
    LeetCode139.单词拆分题目链接:139.单词拆分独上高楼,望尽天涯路没什么思路。慕然回首,灯火阑珊处挖个坑,二刷的时候填。classSolution{public:boolwordBreak......
  • 【LeeCode】300. 最长递增子序列
    【题目描述】给你一个整数数组 ​​nums​​ ,找到其中最长严格递增子序列的长度。子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如......
  • 【LeeCode】674. 最长连续递增序列
    【题目描述】给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。连续递增的子序列 可以由两个下标 ​​l​​ 和 ​​r​​(​​l<r​​)确......
  • 【LeeCode】718. 最长重复子数组
    【题目描述】给两个整数数组 ​​nums1​​ 和 ​​nums2​​ 两个数组中 公共的 、长度最长的子数组的长度 。​​​https://leetcode.cn/problems/maximum-length-......
  • 算法刷题-Excel表列序号、单词拆分 II、排序链表
    Excel表列序号(数学、字符串)给你一个字符串columnTitle,表示Excel表格中的列名称。返回该列名称对应的列序号。例如,A->1B->2C->3...Z->26AA->27AB->......