Leetcode - Reverse Words

比起POJ弱爆了一题,从后往前扫描一遍,O(n)时间,仅仅要注意各种极端情况就可以。不明确通过率为什么仅仅有13%。

#include<iostream>
#include<string>
using namespace std;

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

		char* cstr = new char[s.size()+1];

		int cc = 0;
		int revstrC = s.size() - 1;

		while (revstrC >= 0)
		{
			while (revstrC>=0 && s.at(revstrC) == ‘ ‘)
			{
				revstrC--;
			}

			if (revstrC >= 0)//find the end of a word
			{
				int end = revstrC;

				while (revstrC >= 0 && s.at(revstrC) != ‘ ‘)
					revstrC--;

				s.copy(cstr+cc, end-revstrC, revstrC+1);

				cc = cc + end - revstrC;

				cstr[cc] = ‘ ‘;

				cc++;
			}
		}
		cstr[cc-1] = ‘\0‘;

		s.assign(cstr);
	}
};

int main()
{
	Solution sol;
	string str = "  Hello  fwe  asg   wf      vergv    ";
	sol.reverseWords(str);

	cout << str << endl;
}
时间: 2024-08-03 20:06:07

Leetcode - Reverse Words的相关文章

Leetcode Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode: Reverse Linked List

LeetCode: Reverse Linked List Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follo

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q

LeetCode——Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'

[leetcode]Reverse Words in a String @ Python

原题地址:https://oj.leetcode.com/problems/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". 解题思路:这题很能体现python处理字符串的强大功能. 代码: class Solut

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n sati

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign