[leetcode] 344. Reverse String

原创 2016年05月31日 02:14:23

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

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

Solution

Idea: swap characters

<span style="font-size:14px;">class Solution {
public:
    string reverseString(string s) {
        for(int i=0; i<floor(s.size()/2); i++){
            int j = s.size() - i - 1;
            char ctemp = s[j];
            s[j] = s[i];
            s[i] = ctemp;
        }
        return s;
    }
};</span>

Solution

Idea: rearrange string by inverse order

<span style="font-size:14px;">class Solution {
public:
    string reverseString(string s) {
        int len = s.length();
        string ss = "";
        for (int i=0; i<len; i++)
            ss += s[len-i-1];
        return ss;
    }
};</span>


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode_easy 344、reverse string

344、reverse string//注意c串的结尾 #include #include #include using namespace std; string reverseString(st...

【LeetCode】344. Reverse String

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

【LeetCode】344. Reverse String 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51416284Subject 出处:https://leetcode.com/pro...

<LeetCode OJ> 345 / 344 Reverse Vowels of a String / Reverse String

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy Write a function that takes a stri...

leetcode解题之344. Reverse String & 345. Reverse Vowels of a String Java版(反转字符串)

leetcode解题之344. Reverse String & 345. Reverse Vowels of a String Java版(反转字符串)

344_Reverse String

class Solution { public: string reverseString(string s) { int len = s.length(); ...

[LeetCode]344. Reverse String&345. Reverse Vowels of a String&151. Reverse Words in a String

344. Reverse String 345. Reverse Vowels of a String 151. Reverse Words in a String

36. Valid Sudoku\434. Number of Segments in a String\344. Reverse String\345. Reverse Vowels of a St

Valid Sudoku 题目描述 代码实现 Number of Segments in a String 题目描述 代码实现 Reverse String 题目描述 代码实现 Reverse Vow...

将一个英文句子单词顺序逆置(LeetCode | Reverse Words in a String)

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)