LeetCode(344) Reverse String

題目

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".


分析


字符串逆轉,左右指針。


代碼

class Solution {
public:
	string reverseString(string s) {
		if (s.empty())
			return s;

		int l = 0, r = s.size() - 1;
		string ret = s;
		while (l < r)
		{
			char t = ret[l];
			ret[l] = ret[r];
			ret[r] = t;

			++l;
			--r;
		}
		return ret;
	}
};


發佈了360 篇原創文章 · 獲贊 348 · 訪問量 112萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章