当前位置:网站首页>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
边栏推荐
- LeetCode:497. Random points in non overlapping rectangles -- medium
- redis设置密码命令(临时密码)
- Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
- In depth learning experience and tools
- Is Jiuzhou futures regular? Is it safe to open an account
- Portable FDW framework for Pb
- LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
- Canvas advanced functions (medium)
- The most common habits from more than 200 English papers written by gradua
- Can you still have a wonderful life if you are laid off at the age of 35?
猜你喜欢

Explain L3 cache to solve circular dependency

Lengsuanling, a 30-year tortuous history of IPO of a domestic brand

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

app測試用例

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

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch. cuda. device_ count() is 1.

^29事件循环模型

Canvas advanced functions (medium)

Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"

app测试用例
随机推荐
简解深度学习Attention
魔塔类游戏实现源码及关卡生成
pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
The most common habits from more than 200 English papers written by gradua
Redis缓存雪崩
Redis缓存击穿
LeetCode 进阶之路 - 删除排序数组中的重复项
Niuke.com: numbers that appear more than half of the times in the array
Game compatibility test (general scheme)
分布式服务理论基础
35岁被裁员,还能拥有美妙人生吗?
Is Zhongyan futures a regular platform in China? Is it safe to open an account? Want to open a futures account
pytorch深度学习——神经网络卷积层Conv2d
Redis缓存穿透
冷酸灵,一个国产品牌IPO的30年曲折史
KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
What is the difference between localhost and 127.0.0.1?
知识图谱/关系可视化
How to use Diablo immortal database
Test APK exception control netlocation attacker development