Leetcode详详解

344. Reverse String

2018-11-28  本文已影响0人  Chrisbupt

Leetcode Day 1

344 Reverse String

题目:
Write a function that takes a string as input and returns the string reversed.
编写一个以字符串作为输入并返回反向字符串的函数

Example 1:

Input: "hello" 
Output: "olleh"

Example 2:

Input: "A man, a plan, a canal: Panama"

Output: "amanaP :lanac a ,nalp a ,nam A"

解答:

  1. Python

classSolution(object):defreverseString(self, s):

         ""

         :type s: str

         :rtype: str

        """
    def  reverseString(s):

           return s[::-1]

  1. C++

class Solution {

public:

    string reverseString(string s) {

        int i=0, j=s.size()-1;

        while (i<j){

            swap(s[i++],s[j--]);   //swap函数

        };

        return s;

    }

};

上一篇 下一篇

猜你喜欢

热点阅读