344. Reverse String
2017-02-28 22:24
204 查看
题目
Write a function that takes a string as input and returns the string reversed.Example:
Given s = “hello”, return “olleh”.
Subscribe to see which companies asked this question.
思路
比较简单,两个index往中间靠,并互换字符代码
class Solution { public: void swapChar(string &s,size_t beginIndex,size_t endIndex) { char tempChar = s[beginIndex]; s[beginIndex] = s[endIndex]; s[endIndex] = tempChar; } string reverseString(string s) { size_t length = s.size(); if(length <= 1) { return s; } for(size_t i=0;i< length/2;i++) { swapChar(s,i,length-1-i); } return s; } };
相关文章推荐
- 344. Reverse String
- Leetcode 344 Reverse String 字符串处理
- Easy-题目70:344. Reverse String(增补3)
- [leetcode]344. Reverse String
- 344. Reverse String
- leetcode_easy 344、reverse string
- LeetCode oj 344. Reverse String(字符串)
- 344. Reverse String (转置字符串)
- leetcode note--leetcode 344 Reverse String
- 344. Reverse String
- 344. Reverse String
- 15算法课程 344. Reverse String
- 344. Reverse String 字符串反转
- leetcode--344. Reverse String
- 344. Reverse String
- LeetCode 344. Reverse String(字符串翻转)
- 【LeetCode-344】 Reverse String(C++)
- leetcode 344: Reverse String (c#版)
- [leetcode] 344. Reverse String
- Leet Code OJ 344. Reverse String [Difficulty: Easy]