public class Solution {
    public string ReverseString(string s) {
        var list = s.Reverse();
            StringBuilder sb = new StringBuilder();

            foreach (var c in list)
            {
                sb.Append(c);
            }


            //Console.WriteLine(sb.ToString());
            return sb.ToString();
    }
}

https://leetcode.com/problems/reverse-string/#/description

下面是使用C++的解决方案,不实用api:

class Solution {
public:
    string reverseString(string s) {
        string temp = "";
        for (int i = s.length() - 1; i >= 0; i--)
        {
            temp += s[i];
        }
        return temp;
    }
};

 

这道题目有所变化,原来的要求是返回string,现在的要求是不返回任何值,而是在原来的字符串上直接修改。 下面使用python实现新的要求:

 1 class Solution:
 2     def reverseString(self, s: List[str]) -> None:
 3         """
 4         Do not return anything, modify s in-place instead.
 5         """
 6         n = len(s)
 7         i,j = 0,n-1
 8         while i < j:
 9             s[j],s[i] = s[i],s[j]
10             i += 1
11             j -= 1

 

posted on 2017-04-19 10:52  Sempron2800+  阅读(126)  评论(0编辑  收藏  举报