`
xuerenlv
  • 浏览: 6118 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

leetcode__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".

click to show clarification.

总结:这个看人家的。

class Solution {
public:
   
    void reverseWords(string &s) {
        string rs;
        for (int i = s.length()-1; i >= 0; ){
            while (i >= 0 && s[i] == ' ') i--;
            if (i < 0) break;
            if (!rs.empty()) rs.push_back(' ');
            
            string t;
            while (i >= 0 && s[i] != ' ') t.push_back(s[i--]);
            reverse(t.begin(), t.end());
            rs.append(t);
        }
        s = rs;
    }
};


这个是我的,还没过,继续调试。

class Solution {
public:
    void ReverseWord(string &s,int p, int q){
        while(p < q){
            char t = s[p] ;
            s[p++] = s[q] ;
            s[q--] = t ;
        }
    }
    void reverseWords(string &s) {
        int p = 0;
        int q = 0;
        
        while(s[q] != '\0'){
            if(s[q] == ' '){
                ReverseWord(s,p,q-1);
                q++;
                p=q;
            }else{
                q++;
            }
        }
        
        ReverseWord(s,p,q-1);
        ReverseWord(s,0,q-1);
    }
};


总结:accepted,要对输入的字符串进行处理

class Solution {
public:
    void ReverseWord(string &s,int p, int q){
        while(p < q){
            char t = s[p] ;
            s[p++] = s[q] ;
            s[q--] = t ;
        }
    }
    void reverseWords(string &s) {
        bool flag;
        int p = 0;
        int q = 0;
        
        string str = "";
        for(int i=0;i<s.length();i++)
        {
            flag = false;
            if(s[i] != ' ')
            {
                str += s[i];
            }else if( i-1>=0 && s[i-1] != ' ' ) //前面有
            {
                for(int j=i+1;j<s.length();j++)
                {
                    if(s[j] != ' ')
                    {
                        flag = true;
                        break;
                    }
                }
                if(flag)
                    str += ' ';
            }
        }
        s = str;
        
        while(s[q] != '\0'){
            if(s[q] == ' '){
                ReverseWord(s,p,q-1);
                q++;
                p=q;
            }else{
                q++;
            }
        }
        
        ReverseWord(s,p,q-1);
        ReverseWord(s,0,q-1);
    }
};



分享到:
评论

相关推荐

    Reverse words in a string-leetcode

    Reverse words in a string-leetcode

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    leetcode338-coding_notebook:编码_笔记本

    第 338 章概括 [(雅虎)4。 两个排序数组的中位数](Leetcode 问题/数组和字符串/4.median_of_two_sorted_array.md) [(雅虎)13。...String/151.reverse_words_in_a_string.md) [167. Two Sum 2 - In

    Coding Interview In Java

    2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array ...

    leetcode中国-leetCodeSolution::thumbs_up:leetCode刷题项目

    reverseWords titleToNumber toLowerCase defangIPaddr replaceSpace removeOuterParentheses 复杂题目值得参考 sortString 使用字典排序 sort((a, b) =&gt; a.charCodeAt() - b.charCodeAt()) 有参考价值 Maths isPal

    leetcode写题闪退-LeetCode:leetcodeOJ

    leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...

    leetcode2sumc-Leetcode-2020:刷刷Leetcode并作纪录

    leetcode 2 sum c Leetcode 练习记录 这个专案主要存放我练习Leetcode有针对难度分类的集合题库(Collection Question) 准备方式 分析tag的热门标签,熟悉各个标签解题的思路(解决该标签全部的easy和medium为主),再...

    TWDH#Leetcode-From-Zero#13.反转字符串中的单词 III1

    557. 反转字符串中的单词 IIIpublic String reverseWords(String s) {StringBuilder sb = new S

    leetcode-go:我使用Golang解决LeetCode问题的方法

    goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除有序数组...

    leetcode字符串括号level-leetcode:LeetCode解题记录

    reverseWords 翻转字符串里的单词 simplifyPath 简化路径 restoreIPAddresses 复原IP地址 threeSum 三数之和 search 搜索旋转排序数组 1. 3. 9. 75. 209. 219. 167. 268. 344. 349. 454. 447. 695. 674. string 字符...

    LeetCode 151 – 翻转字符串里的单词

    题目描述 151. 翻转字符串里的单词 解法一:(Python) class Solution: def reverseWords(self, s: str) -&gt; str: return " ".join(reversed(s.split())) 解法二:双端队列(C++) ... string reverseWords(strin

    cpp-算法精粹

    Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer...

Global site tag (gtag.js) - Google Analytics