当前位置:网站首页>LeetCode 进阶之路 - 反转字符串
LeetCode 进阶之路 - 反转字符串
2022-06-10 20:01:00 【Li_XiaoJin】
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
示例 1:
输入:["h","e","l","l","o"]
输出:["o","l","l","e","h"]
示例 2:
输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
- 双指针法双指针法是使用两个指针,一个左指针 left,右指针 right,开始工作时 left 指向首元素,right 指向尾元素。交换两个指针指向的元素,并向中间移动,直到两个指针相遇。
public class ReverseString {
public static void reverseString(char[] s) {
int head = 0;
int tail = s.length-1;
while (head < tail) {
char temp = s[head];
s[head] = s[tail];
s[tail] = temp;
head++;
tail--;
}
}
/**
* 这个是别人的
* 进行字符串重置
* @param charArray
*/
public static void reserve(char[] charArray) {
int strMin = charArray.length >> 1;
int start = 0;
int lasttag = charArray.length-1;
while (start < strMin) {
swap(charArray, start, lasttag - start++);
}
}
/**
* 交换数组中两个数的位置
* 通过位亦或来进行量变量交换
* @param arr 需要交换的数组
* @param i 第一个要交换的元素的数组下标
* @param j 第二个要交换的元素的数组下标
*/
private static void swap(char[] arr, int i, int j) {
// 这里我还搞不懂啥意思,要查查。。。
arr[i] = (char) (arr[i] ^ arr[j]);
arr[j] = (char) (arr[i] ^ arr[j]);
arr[i] = (char) (arr[i] ^ arr[j]);
}
public static void main(String[] args) {
char[] s = new char[]{'h','e','l','l','o'};
reverseString(s);
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/2020-09-09-16-17-48
边栏推荐
- vulnhub-The Planets: Earth
- 从h264实时流中提取Nalu单元数据
- The most common habits from more than 200 English papers written by gradua
- [technical fragment] implementation of renaming and filtering duplicate name files with suffixes
- Knife4j configuration can use direct copy
- 堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
- Elastic-Job的快速入门,三分钟带你体验分布式定时任务
- Mixin -- mixed
- ^29 event cycle model
- Unity analyzes the rendering of built-in terrain and does some interesting things
猜你喜欢

Service management and communication, basic principle analysis

六级考试-商务英语-考前最后一背

In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table

知识图谱/关系可视化

pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)

^29事件循环模型

Microsoft Word tutorial, how to change page orientation and add borders to pages in word?

Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解

View play and earn will lead crypto games astray

Attack and defense drill | network security "whistleblower": security monitoring
随机推荐
Is Zhongyan futures a regular platform in China? Is it safe to open an account? Want to open a futures account
Identity and access management (IAM)
从h264实时流中提取Nalu单元数据
Steps to build a personal website using servers purchased by Alibaba cloud international
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
Redis缓存击穿
Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
Codeforces Round #798 (Div. 2)
Zero trust architecture
Leetcode divides the array so that the maximum difference is k
mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
聊聊服务器性能优化~(建议收藏)
^30h5 web worker multithreading
Unity analyzes the rendering of built-in terrain and does some interesting things
pytorch深度学习——卷积操作以及代码示例
保姆级教程:如何成为Apache Linkis文档贡献者
AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
Diablo immortal wiki address Diablo immortal database address sharing
简解深度学习Attention
Redis缓存雪崩