reverse string | leetcode

思路:在原来的字符串后面添加上strlen-1个字符,返回

class Solution {
public:
    string reverseString(string s) {
        unsigned int strlen;
        int i;
        // char temp;  

        strlen = s.size();
        if (strlen > 1) {
            for (i = strlen - 2; i >= 0 ; i--)   {
                // temp = s[i];
                s += s[i];
            }
            return s.substr(strlen - 1 , strlen + strlen - 2);
        }
        else
            return s;
    }
}; 
时间: 2024-10-15 13:47:25

reverse string | leetcode的相关文章

[LeetCode] 344 Reverse String & 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

Reverse Words in a String leetcode java

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters con

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] Reverse String 翻转字符串

Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 这道题没什么难度,直接从两头往中间走,同时交换两边的字符即可,参见代码如下: 解法一: class Solution { public: string reverseString(string s) { int left =

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] Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or eq

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

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

Nim Game,Reverse String,Sum of Two Integers

下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take t