天天看點

LeetCode 344 Reverse String

版權聲明:轉載請聯系本人,感謝配合!本站位址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51315232

傳回字元串的逆序

原文

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

Example:

Given s = “hello”, return “olleh”.

翻譯

寫一個函數,以一個字元串作為輸入,并逆序傳回。

例如:

給定s = “hello”,傳回”olleh”。

代碼

直接逆序周遊

string reverseString(string s) {
    string result;
    for (int i = s.length() - 1; i >= 0; i--) {
        result += s[i];
    }
    return result;
}           

使用STL

string reverseString(string s) {
    reverse(s.begin(), s.end());
    return s;
}           

Copyright

http://creativecommons.org/licenses/by-nc-sa/4.0/
LeetCode 344 Reverse String

Nomasp by

Ke Yuwang

is licensed under a

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

.

Permanent Link:

nomasp.com

, ALL RIGHTS RESERVED.

繼續閱讀