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来存放结果。

1
2
3
4
5
6
7
8
9
10
11
12
class  Solution {
public :
     string reverseString(string s) {
         int  len = s.size();
         string result;
         for ( int  n = 0; n < len; n++)
         {
             result.append(1,s.at(len - 1 - n));
         }
         return  result;
     }
};

思路2:

修改原来string直接得到结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
class  Solution {
public :
     string reverseString(string s) {
         int  len = s.size();
         for  ( int  i = 0; i < len / 2 ; i++)
         {
             char  a = s[i];
             s[i] = s[len - 1 - i];
             s[len - 1 - i] = a;
         }
         return  s;
     }
};




本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1836488

相关文章
|
8月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
464 100
|
8月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
620 99
|
8月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
8月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
9月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
435 92
|
10月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
488 14
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
425 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
227 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
516 2