当前位置:网站首页>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
边栏推荐
- 2 pcs share a set of keyboard and mouse
- 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?
- Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
- Codeforces Round #798 (Div. 2)
- Test APK exception control netlocation attacker development
- 【电脑使用】如何设置没有自启项的软件开机启动
- Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
- torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
- Will your company choose to develop data center?
- LeetCode 进阶之路 - 字符串中的第一个唯一字符
猜你喜欢

What is the difference between localhost and 127.0.0.1?

Redis缓存雪崩

pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)

自注意力(self-attention)和多头注意力(multi-head attention)

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation

你的公司会选择开发数据中台吗?

电子招标采购商城系统:优化传统采购业务,提速企业数字化升级

2台电脑共享一套键盘鼠标

^29 event cycle model
随机推荐
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
LeetCode:497. 非重叠矩形中的随机点————中等
H265 Nalu类型判断及 sps 数据解析
Niuke.com: numbers that appear more than half of the times in the array
聊聊服务器性能优化~(建议收藏)
信号与系统复习1
pytorch深度学习——卷积操作以及代码示例
Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
^29事件循环模型
Brute force method / adjacency table depth first directed weighted graph undirected weighted graph
Is Jiuzhou futures regular? Is it safe to open an account
Codeforces Round #798 (Div. 2)
Read the source code of micropyton - add the C extension class module (3)
面试必备——synchronized底层原理的基础知识
一、Vulkan开发理论基础知识
synergy: server refused client with our name
pdf.js-----js解析pdf文件實現預覽,並獲取pdf文件中的內容(數組形式)
LeetCode 进阶之路 - 136.只出现一次的数字
MySQL service startup failed