[LeetCode 第3题] -- Reverse Words in a String

题目链接: Reverse Words in s String

题目意思: 给定一个字符串,要求把字符串中的"单词"反转

                1. 所谓"单词"指的的连续的非空白字符

                2. 必须把前后缀空格去掉

                3. 连续多个空格要合并为一个

代码

class Solution {
public:
    void reverseWords(string &s);
};

void Solution::reverseWords(string &s) {
     int stringSize = s.length();
     if (stringSize == 0) {
         return;
     }
     int begin = 0;
     int end = stringSize-1;
     while (s[begin] == ' ' && (begin < stringSize)){
         ++begin;
     }
     while (s[end] == ' ' && (end >= 0)){
         --end;
     }
     string tmpString = "";
     for (int i = begin; i <= end; ++i) {
         tmpString = tmpString + s[i];
     }
     s = "";
     int tmpStringSize = tmpString.length();
     begin = 0;
     end = tmpStringSize;
     for (int i = tmpStringSize-1; i >= 0; --i) {
         if (tmpString[i] == ' ' || i == 0) {
            if (tmpString[i] == ' ') {
               begin = i;
            }
            else {
               begin = -1;
            }
            for (int j = begin+1; j < end; ++j) {
                s = s + tmpString[j];
            }
            if (begin != -1 && (end-begin > 1)) {
                s = s + ' ';
            }
            end = begin;
      }
}
时间: 2024-08-01 22:45:04

[LeetCode 第3题] -- Reverse Words in a String的相关文章

[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. click to show clarification.

[LeetCode 第4题] -- Evaluate Reverse Polish Notation

题目链接: Evaluate Reverse Polish Notation 题目意思: 给的一个表达式,求值 代码: class Solution { public: bool IsOperator(const string &str); int GetStackTop(stack<int> &stack); int evalRPN(vector<string> &tokens); }; bool Solution::IsOperator(const st

[LeetCode] Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". 解题思路 双指针,一个往前移动,一个往后移动,找到元音字母时

[LeetCode]--345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". Note: The vowels does not incl

[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". Clarification: 1.What constitutes a word? A sequence of non-space characters constitutes a word. 2.Could the in

[leetCode 第2题] -- Maximum Product Subarray

题目链接 题目意思: 给定一个数组,求最大连续子序列的乘积 分析: 简单dp.maxDp[i]表示以num[i]结尾的最大子序列乘积,minDp[i]类似          maxDp[i] = max{num[i], maxDp[i-1]*num[i], minDp[i-1]*num[i]}          minDp[i] = min{num[i], minDp[i-1]*num[i], maxDp[i-1]*num[i]}          之所以要minDp,是因为有负数的存在,导致多

[LeetCode 第10题] -- Linked List Cycle

题目链接: linked List Cycle 题目意思: 给定一个链表,判断链表是否有环 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head); }; b

[LeetCode 第11题] -- Linked List Cycle II

题目链接: Linked List Cycle II 题目意思: 给定一个链表,如果链表有环求出环的起点,否则返回NULL 解题思路:      1. 判断链表是否有环: 两个指针,一个一次走一步,一个一次走两步,如果指针相遇说明有环,否则无环.     2. 如果有环的情况下,我们可以画个图(图片来自网络)                   假设两个指针在z点相遇.则          a. 指针1走过路程为a + b:指针2走过的路程为 a+b+c+b          b. 因为指针2的

[leetCode 第1题] -- Find Minimum in Rotated Sorted Array

题目链接: Find Minimun in Rotated Sorted Array 题目意思: 给定一个旋转数组,找出这个旋转数组的最小元素.旋转数组的定义是,把一个从小到大排好序的数组,取一部份放到末尾,例如0 1 2 4 5 6 7 取 0 1 2放到末尾,变成旋转数组4 5 6 7 0 1 2 代码:  class Solution { public: int findMin(vector<int> &num); }; int Solution::findMin(vector&