[leetcode]: 344. Reverse String

来源:互联网 发布:手机淘宝哪里实名认证 编辑:程序博客网 时间:2024/06/14 04:52

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

Example:
Given s = “hello”, return “olleh”.
翻转字符串,很直白的题目
2.代码
python
宝宝终于会偷懒了,深感欣慰啊

def reverseString(self, s):    return s[::-1]

c++

    string reverseString(string s) {        int i = 0;        int j = s.length() - 1;        while (i < j) {            char temp = s[i];            s[i] = s[j];            s[j] = temp;            ++i;            --j;        }        return s;    }
0 0
原创粉丝点击