leetcode_344 Reverse String

  • 题目分析:

    对于给定的字符串,执行逆转操作。

  • 解题思路:

    先统计字符串的长度,然后遍历字符串,将字符串的前后元素一一对调即可实现。

  • 实现程序
    • C++版本

      class Solution {
      public:
          // 字符交换操作
          void my_swap(char *s, char *t)
          {
              char temp = *s;
              *s = *t;
              *t = temp;
          }
          // 字符串逆转操作
          string reverseString(string s) {
              int length = s.size();
              int i = 0;
              int j = length - 1;
              // 遍历执行逆转
              while (i < j)
              {
                  my_swap(&s[i], &s[j]);
                  i++;
                  j--;
              }
              return s;
          }
      };
      
    • C版本
      // 字符交换操作
      void swap(char *s, char *t)
      {
          char temp = *s;
          *s = *t;
          *t = temp;
      }
      // 统计字符串长度
      int length(char *s)
      {
          int len = 0;
          while (s[len] != ‘\0‘)
              len++;
          return len;
      }
      // 字符串逆转操作
      char *reverseString(char *s)
      {
          int len = length(s);
          int i = 0;
          int j = len - 1;
          while (i < j)
          {
              swap(&s[i], &s[j]);
              i++;
              j--;
          }
          return s;
      }
      
    • Java版本
      public class Solution {
          public String reverseString(String s) {
              // 先将字符串转换为字符数组
              char[] array = s.toCharArray();
              int i = 0;
              int j = s.length() - 1 ;
              char temp;
              // 逆转字符数组
              while (i < j){
                  temp = array[i];
                  array[i] = array[j];
                  array[j] = temp;
                  i++;
                  j--;
              }
              // 利用逆转的字符数组构造最终的逆转字符串
              return new String(array);
          }
      }
      
时间: 2024-08-01 18:40:20

leetcode_344 Reverse String的相关文章

[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

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为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

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

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

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

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