当前位置:网站首页>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());}}; 边栏推荐
猜你喜欢

Redis 5 种数据结构及对应使用场景

一款好用的FAQ搭建工具

Redis cluster configuration

In action: 10 ways to implement delayed tasks, with code!

Five data structures of Redis and their corresponding usage scenarios

SCANIA SCANIA OTL tag is introduced

模板的进阶

4 kmiles join YiSheng group, with more strong ability of digital business, accelerate China's cross-border electricity full domain full growth

Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)

Flutter with internationalized adapter automatically generated
随机推荐
光源控制器接口定义说明
即时通讯开发移动端网络短连接的优化手段
当TIME_WAIT状态的TCP正常挥手,收到SYN后…
溜不溜是个问题
Five data structures of Redis and their corresponding usage scenarios
SQL Server实现group_concat功能
银保监会:人身险产品信披材料应由保险公司总公司统一负责管理
让你的应用完美适配平板
【软件工程导论】软件工程导论笔记
golang刷leetcode动态规划(11)不同路径
golang面试题
腾讯云孟凡杰:我所经历的云原生降本增效最佳实践案例
遇上Mysql亿级优化,怎么办
AI科学家:自动发现物理系统的隐藏状态变量
LeetCode:622. 设计循环队列【模拟循环队列】
MaxCompute 的SQL 引擎参数化视图具体有哪些增强功能?
ImageNet下载及处理
Shell: conditional statements
Redis cluster configuration
shell:条件语句