344 Reverse String 反转字符串

请编写一个函数,其功能是将输入的字符串反转过来。
示例:
输入:s = "hello"
返回:"olleh"
详见:https://leetcode.com/problems/reverse-string/description/
C++:

class Solution {
public:
    string reverseString(string s) {
        int n=s.size();
        if(n==0||s.empty())
        {
            return "";
        }
        int left=0,right=n-1;
        while(left<right)
        {
            char tmp=s[left];
            s[left]=s[right];
            s[right]=tmp;
            ++left;
            --right;
        }
        return s;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8836357.html

时间: 2024-10-21 19:09:23

344 Reverse String 反转字符串的相关文章

Leetcode 344:Reverse String 反转字符串(python、java)

Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit

leetCode 344. Reverse String 字符串

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 思路1: 使用一个新的string来存放结果. class Solution { public:     string reverseString(string s) {        

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

344. Reverse String(C++)

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 题目大意: 字符串倒置. 解题方法: 第一个字符与最后一个非空字符对换. 注意事项: 1.字符串最后一个字符是空字符. C++代码: 1.不良代码: 1 class Solution

Leetcode刷题记录[python]——344 Reverse String

一.前言 不是计算机专业出身,却有一颗程序猿的心. 昨日开始leetcode第一次刷题,选择了菜鸟方式,从AC率最高且难度为Easy的题开始,不管题是简单还是难,都想做个记录,既是方便以后回顾,又是以此作为一个激励,督促自己每天都能有所进步. 二.题344 Reverse String Write a function that takes a string as input and returns the string reversed. class Solution(object): def

344. Reverse String【easy】

344. Reverse String[easy] Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 解法一: 1 class Solution { 2 public: 3 string reverseString(string s) { 4 int start = 0, e

【leetcode】344. Reverse String

problem 344. Reverse String 参考 1. Leetcode_344_Reverse String; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10419862.html

LeetCode之344. Reverse String

------------------------------- Java也可以实现一行代码反转字符串哦 AC代码如下: public class Solution { public String reverseString(String s) { return new StringBuffer(s).reverse().toString(); } } 题目来源: https://leetcode.com/problems/reverse-string/

344. Reverse String

题目:反转字符串 Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 五种解法: #直接逆序 class Solution(object): def reverseString(s): return s[::-1] 耗时56ms #前后对称位互换 class Solution(