当前位置:网站首页>力扣344-反转字符串——双指针法
力扣344-反转字符串——双指针法
2022-07-30 05:09:00 【张怼怼√】
问题描述
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
问题求解
- 由于要反转整个字符数组,所以便联想到双指针求解;
- 建立两个指针first指向数组首部,last指向数组尾部;
- 每循环一次,交换两个指针所指向的字符,直至遍历完整个数组。
- 这道题属于非常简单的题,用于巩固。
输入输出示例
代码
class Solution {
public void reverseString(char[] s) {
int len = s.length;
int first = 0;
int last = len - 1;
char tem;
while(first <= last){
tem = s[first];
s[first] = s[last];
s[last] = tem;
first++;
last--;
}
}
}
边栏推荐
- Requirements design document and the changing role of the product manager
- 全流程调度——Azkaban入门与进阶
- Boss Rush (two-point answer + DP)
- Hexagon_V65_Programmers_Reference_Manual(12)
- Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds
- Catch That Cow(详解)
- 盘点 | 常用 PG 数据恢复方案概览【建议收藏】
- 小程序使用npm包定制全局样式
- Catch That Cow (detailed)
- Shi Xingguo, founder of Hyperchain, was interviewed by 21st Century Business Herald to interpret Shanghai's new NFT regulations and digital development
猜你喜欢
随机推荐
盘点 | 常用 PG 数据恢复方案概览【建议收藏】
Predictive maintenance scheduling of multiple power equipment based on data-driven fault prediction
The Double Pointer Problem (Part 1)
Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
Unity stepping on the pit record - the use of GetComponent
C language implements highly secure game archives and reads files
Catch That Cow(详解)
Simulation problem (below)
Simulation Problem (Part 1)
[Verilog] HDLBits Problem Solution - Circuits/Combinational Logic
String problem (below)
Whole process scheduling - Azkaban entry and advanced
聊一聊什么是SaaS,以及遇到的问题......
GO语言学习笔记一
Usage when saving pointers in std::vector
Unity踩坑记录 —— GetComponent的使用
涂鸦Wi-Fi&BLE SoC开发幻彩灯带
POJ1321 棋盘问题(详解)
“幻核”跑路,数字藏品路在何方?
光明区关于促进科技创新的若干措施(征求意见稿)