当前位置:网站首页>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
边栏推荐
- PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
- How to use Diablo immortal database
- MySQL service startup failed
- shell实现ssh登录并执行命令
- Use DAP link to download the executable file separately to the mm32f5 microcontroller
- LeetCode:497. 非重叠矩形中的随机点————中等
- Li Kou 10821084 solution_ Question of SQL query type
- 蛮力法/1~n个整数中取k个整数
- Steps to build a personal website using servers purchased by Alibaba cloud international
- NetWorkX使用方法及 nx.draw()相关参数
猜你喜欢

app测试用例

mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)

pytorch深度学习——卷积操作以及代码示例

LeetCode:1037. 有效的回旋镖————简单

Fast Planner - detailed explanation of kinetic astar

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

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

Redis缓存穿透

app測試用例

简解深度学习Attention
随机推荐
记录一下今天的MySQL故障
The most common habits from more than 200 English papers written by gradua
app測試用例
Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
You have to learn math to play art?
Power set V4 recursion of brute force method /1~n
Solve the problem that the idea automatically becomes * when there are more than 5 identical packages
Portable FDW framework for Pb
72. editing distance ●●
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
Read the source code of micropyton - add the C extension class module (3)
H5 van-popup全屏弹窗,模拟页面回退效果,支持左上角返回按钮,适用物理返回,侧滑与底部返回键
Nanny tutorial: how to become a contributor to Apache linkis documents
Read the source code of micropyton - add the C extension class module (1)
ros虚拟时间
自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
1、 Vulkan develops theoretical fundamentals