leetcode151. 翻转字符串里的单词

给定一个字符串,逐个翻转字符串中的每个单词。

示例 1:

输入: "the sky is blue"
输出: "blue is sky the"

示例 2:

输入: "  hello world!  "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。

示例 3:

输入: "a good   example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答;

这里吐槽一句:别用坑爹的spilt函数,真的坑死了,还是自己写一个用吧,其他没什么,就是将字符串分割放入一个栈中,最后在取出来就倒序了。

 1 class Solution {
 2     public String reverseWords(String s) {
 3         if(s==null||s.length()==0)
 4             return s;
 5         LinkedList<String> list=new LinkedList<>();
 6         helper(list,s);
 7         if(list.isEmpty())
 8             return "";
 9         StringBuilder sb=new StringBuilder();
10         while(!list.isEmpty())
11             sb.append(list.pop()+" ");
12         return sb.toString().substring(0,sb.length()-1);
13
14
15     }
16     public void helper(LinkedList<String>list,String s)
17     {
18         int r=0;
19         int l=0;
20         boolean flag=false;
21         while(r<s.length())
22         {
23             if(s.charAt(r)==‘ ‘&&flag==true)
24             {
25                 String str=s.substring(l,r);
26                 list.push(str);
27                 flag=false;
28                 r++;
29             }
30             else if(s.charAt(r)==‘ ‘&&flag==false)
31             {
32                 r++;
33             }
34             else if(s.charAt(r)!=‘ ‘&&flag==false)
35             {
36                 flag=true;
37                 l=r;
38                 r++;
39             }
40             else
41                 r++;
42         }
43         if(flag)
44             list.push(s.substring(l,r));
45     }
46 }

原文地址:https://www.cnblogs.com/cold-windy/p/11875631.html

时间: 2024-07-29 23:26:12

leetcode151. 翻转字符串里的单词的相关文章

leetcode python翻转字符串里的单词

# Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: "blue is sky the" **示例2:** 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. **示例3:** 输入: "

151 Reverse Words in a String 翻转字符串里的单词

给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用O(1) 时间复杂度的原地解法.说明:    什么构成一个词?    一系列非空格字符组成一个词.    输入字符串是否可以包含前导或尾随空格?    是.但是,您的反转字符串不应包含前导或尾随空格.    两个单词之间多空格怎么样?    将它们缩小到反转字符串中的单个空格.详见:https://leetc

LeetCode 翻转字符串里的单词

给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: "  hello world!  " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 示例 3: 输入: "a good   example" 输出: "examp

LeetCode 151 翻转字符串里的单词

题目: 给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: "  hello world!  " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. 示例 3: 输入: "a good   example" 输出: "e

151.翻转字符串里的单词

class Solution: def reverseWords(self, s: str) -> str: if s == '': return s ls = s.split() # " " if ls == []: return '' result = '' for i in range(0, len(ls)): result += ls[-1 - i] + ' ' # strip()去除首尾空格 result = result.strip() return result 原

翻转字符串里的单词

试题地址:https://leetcode-cn.com/problems/reverse-words-in-a-string/ 试题思路: go自带strings.Fields()函数,可以剔除多余空格 试题代码: func reverseWords(s string) string { strList := strings.Fields(s) if len(strList) == 0 { return "" } reverseS := "" for i := l

Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode

LeetCode新题,但是比较简单,直接用栈即可 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". click to show clarification. Clarification: What constitutes a word?A sequence of non-sp

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t