当前位置:网站首页>Leetcode advanced path - reverse string
Leetcode advanced path - reverse string
2022-06-10 21:18:00 【Li_ XiaoJin】
Write a function , Its function is to invert the input string . Input string as character array char[] Given in the form of .
Do not allocate extra space to another array , You have to modify the input array in place 、 Use O(1) To solve this problem .
You can assume that all characters in an array are ASCII Printable characters in code table .
Example 1:
Input :["h","e","l","l","o"]
Output :["o","l","l","e","h"]
Example 2:
Input :["H","a","n","n","a","h"]
Output :["h","a","n","n","a","H"]
- Double pointer method double pointer method uses two pointers , A left pointer left, Right pointer right, When starting work left Point to first element ,right Point to the tail element . Exchange the elements pointed to by two pointers , And move in the middle , Until the two hands meet .
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--;
}
}
/**
* This is someone else's
* Perform a string reset
* @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++);
}
}
/**
* Swap the positions of two numbers in an array
* The exchange of variables by bits or
* @param arr Arrays that need to be swapped
* @param i The array subscript of the first element to be exchanged
* @param j The array subscript of the second element to be exchanged
*/
private static void swap(char[] arr, int i, int j) {
// I don't know what it means here , To check ...
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: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/2020-09-09-16-17-48
边栏推荐
- CET-6 - Business English - the last recitation before the test
- 連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
- 自注意力(self-attention)和多头注意力(multi-head attention)
- Theoretical basis of distributed services
- MySQL service startup failed
- Obtained network time + time zone (+8)
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
- The programmed navigation route jumps to the current route (the parameters remain unchanged), and the navigationduplicated warning error will be thrown if it is executed multiple times?
- 蛮力法/1~n的幂集 v4 递归
猜你喜欢

CET-6 - Business English - the last recitation before the test
![[generation confrontation network learning part I] classic Gan and its existing problems and related improvements](/img/5a/0a4015cd4dcc21afd16ca7f895d909.png)
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements

What should be paid attention to when designing Multilayer PCB?

Theoretical basis of distributed services

LeetCode:1037. Effective boomerang - simple

Nanny tutorial: how to become a contributor to Apache linkis documents

连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors

异步、线程池(CompletableFuture)

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

Cas de test app
随机推荐
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
在YUV图像上根据背景色实现OSD反色
Read the source code of micropyton - add the C extension class module (2)
ros虚拟时间
Vissim仿真快速入门
在手机上买基金安全吗?会不会被吞本金?
Zero trust architecture
Game compatibility test (general scheme)
Elastic-Job的快速入门,三分钟带你体验分布式定时任务
Meetup预告:Linkis新版本介绍以及DSS的应用实践
Test APK exception control netlocation attacker development
leetcode 划分数组使最大差为 K
实用 | 如何利用 Burp Suite 进行密码爆破!
Will your company choose to develop data center?
Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
蛮力法/邻接表 深度优先 有向带权图 无向带权图
连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
揭秘:春晚微信红包,是如何抗住 100 亿次请求的?