当前位置:网站首页>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());}};
边栏推荐
猜你喜欢
随机推荐
所谓武功再高也怕菜刀-分区、分库、分表和分布式的优劣
Fiddle设置接口数据用指定工具查看;Sublime Text设置json数据格式化转换
APP自动化uiautomator2获取toast
Lvm逻辑卷
我用这一招让团队的开发效率提升了 100%!
软考 ----- UML设计与分析(下)
软件测试分类
Triacetin是什么化学材料
GNN教程:图神经网络基础知识!
Office2021 安装MathType
SQL Server数据类型转换函数cast()和convert()详解
Electron使用指南之初体验
Meta 与苹果的元宇宙碰撞
Cannot find declaration to go to
golang 源码分析:juju/ratelimit
golang刷leetcode动态规划(11)不同路径
网络协议介绍
Leetcode刷题——单调栈问题(739每日温度问题、496下一个更大元素I、503下一个更大元素 II)
清除浮动与BFC
Redis集群配置