当前位置:网站首页>Likou Question of the Day - Day 46 - 344. Reverse Strings
Likou Question of the Day - Day 46 - 344. Reverse Strings
2022-08-02 23:02:00 【Heavy Mail Research Sen】
2022.8.2 Did you write the questions today?
Title:
Write a function that reverses the input string.The input string is given as a character array s.
Don't allocate extra space for another array, you have to modify the input array in place, using O(1) extra space to solve this problem.
Analysis:
Given a vector array of char, no new space memory can be generated to reverse the original string.
Ideas: The simplest idea: a reverse!!!Of course, that makes no sense.So our idea is:
Use two pointers, one from the left and one from the right, and use a temp as an intermediate variable.Swap two values until left >= right.
Analysis:
1. Double pointer
class Solution {public:void reverseString(vector& s) {int left = 0;int right = s.size() - 1;char temp;while (left < right){temp = s[left];s[left] = s[right];s[right] = temp;left++;right--;}}};
2.reverse
class Solution {public:void reverseString(vector& s) {reverse(s.begin(),s.end());}};
边栏推荐
猜你喜欢
随机推荐
Compose主题切换——让你的APP也能一键换肤
Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
【LeetCode】1161. 最大层内元素和
golang 源码分析:uber-go/ratelimit
扫码预约 | 观看Apache Linkis数据处理实践以及计算治理能力
程序员也许都缺一个“二舅”精神
Translate My Wonderful | July Moli Translation Program Winners Announced
Lvm逻辑卷
顺序查找和折半查找,看这篇就够了
ALV概念讲解
Mysql安装流程 【压缩版】
TodoList案例
ECCV 2022 | 通往数据高效的Transformer目标检测器
腾讯云孟凡杰:我所经历的云原生降本增效最佳实践案例
golang刷leetcode动态规划(11)不同路径
ShardingSphere-proxy +PostgreSQL实现读写分离(静态策略)
Three.js入门
7月29-31 | APACHECON ASIA 2022
ShapeableImageView 的使用,告别shape、三方库