leetcode 344 Reverse String

来源:互联网 发布:java startwith 正则 编辑:程序博客网 时间:2024/05/29 19:20

1. 题目分析

  题目描述:Write a function that takes a string as input and returns the string reversed.
    Example: Given s = “hello”, return “olleh”.
  题目含义:反转字符串
  题目分析:从后之前取原串的元素并赋给新串即可。

2. 题目解答

class Solution {public:    string reverseString(string s) {        if (s.size() == 0) {            return "";        }        string newstr;        for (int i = s.size()-1; i >= 0; --i) {            newstr += s[i];        }        return newstr;    }};

3. 心得体会

  在对串进行遍历时,注意 i 的取值范围。