leetcode --344. Reverse String

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 i = 0 ;
 5             int j = s.size()-1;
 6             while(i<j)
 7             {
 8                 char temp = s[i];
 9                 s[i++] = s[j];
10                 s[j--] = temp;;
11
12             }
13             return s;
14     }
15 };
 1 class Solution{
 2 public:
 3     string reverseString(string s){
 4         int i , j , n;
 5         n = strlen(s);
 6         char* tmp = (char*)malloc(sizeof(char)*(n+1));
 8         for(i=n,j=0;i>0;i--,j++)
 9            tmp[j] = s[i-1];
11         tmp[n] = ‘\0‘;
13         return tmp ;
14     }
15 };
时间: 2025-01-04 11:58:54

leetcode --344. Reverse String的相关文章

[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

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) {        

Python [Leetcode 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(self, s): """ :type s: str :rtype

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

【leetcode】344. Reverse String

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

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

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

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

LeetCode笔记:344. Reverse String

问题: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 大意: 写一个函数获取输入的字符串然后返回反转后后的字符串. 比如: 给出s = "hello",返回"olleh" 思路: 思路很直接就想到,先把字符串拆分成一个个字符组成的