版权声明:本文章为博主原创,转载请注明出处。保留所有权利。

Contents
  1. 1. 题目
  2. 2. 解析

Leetcode-344的解题过程。

  • 题目

    Write a function that takes a string as input and returns the string reversed.

    1
    2
    Example:
    Given s = "hello", return "olleh".
  • 解析

    一道很简单的题目,反转字符串,直接遍历前半段字符串,然后交换当前字符和后半段对称位置的字符就可以了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class Solution {
    public:
    string reverseString(string s) {
    //若s的长度是奇数,那么中间的字符无需交换,遍历至s/lenth()/2(地板除)刚好
    for (int i=0;i<s.length()/2;i++){
    char temp = s[i];
    s[i] = s[s.length()-i-1];
    s[s.length()-i-1] = temp;
    }
    return s;
    }
    };

打赏

取消
扫码支持

你的支持是对我最好的鼓励

Contents
  1. 1. 题目
  2. 2. 解析