当前位置:网站首页>LeetCode_ String_ Simple_ 344. reverse string
LeetCode_ String_ Simple_ 344. reverse string
2022-06-12 11:56:00 【I've been up and down in the Jianghu】
1. subject
Write a function , Its function is to invert the input string . Input string as character array s 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 .
Example 1:
Input :s = [“h”,“e”,“l”,“l”,“o”]
Output :[“o”,“l”,“l”,“e”,“h”]
Example 2:
Input :s = [“H”,“a”,“n”,“n”,“a”,“h”]
Output :[“h”,“a”,“n”,“n”,“a”,“H”]
Tips :
1 <= s.length <= 105
s[i] All are ASCII Printable characters in code table
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-string
2. Ideas
(1) Symmetric exchange
Definition i and j, The initial values are 0 and s.length - 1, Then start exchanging s[i] and s[j] Value , Every time I exchange ,i++,j- -, until i >= j End exchange at , At this point, the string is successfully inverted .
3. Code implementation (Java)
// Ideas 1———— Symmetric exchange
class Solution {
public void reverseString(char[] s) {
int length = s.length;
for (int i = 0, j = length - 1; i < j; i++, j--) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
边栏推荐
- Reentrantlock source code analysis
- Design of secure chat tool based on C #
- IP地址管理
- QT添加QObject类(想使用信号和槽)遇到的问题汇总,亲测解决有效error: undefined reference to `vtable for xxxxxx(你的类名)‘
- 必杀技--使用FFmpeg命令快速精准剪切视频
- QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘
- ARM指令集之伪指令
- 【深度学习基础】反向传播法(1)
- Video JS library uses custom components
- Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1
猜你喜欢

6.6 Convolution de séparation

Lambda and filter, List 和 numpy array的索引,以及各种距离指标distance-metrics,拼接数组以及axis=0 and axis=1的区分

MySQL - built in function

Manuscript manuscript format preparation

【深度学习基础】神经网络的学习(4)

异步路径处理
![[foundation of deep learning] back propagation method (1)](/img/0b/540c1f94712a381cae4d30ed624778.png)
[foundation of deep learning] back propagation method (1)

IP地址管理

LeetCode 890. Find and replace mode (analog + double hash table)

视频分类的类间和类内关系——正则化
随机推荐
LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)
IP地址管理
Rich text editor copying pictures in word documents
Doris记录服务接口调用情况
Reasons for SSL introduction and encryption steps
异步路径处理
Pytorch笔记
Why is there no traffic after the launch of new products? How should new products be released?
QML学习 第二天
Lambda and filter, List 和 numpy array的索引,以及各种距离指标distance-metrics,拼接数组以及axis=0 and axis=1的区分
必杀技--使用FFmpeg命令快速精准剪切视频
Cookies and sessions
Relation entre les classes et à l'intérieur des classes de classification vidéo - - Régularisation
寻找两个有序数组的中位数(LeetCode 4)
Unlimited growth, we will all go to the future | the 15th anniversary of the founding of InfoQ China
Reprint --win10 open the task manager to solve the blue screen problem
Batch load/store instructions of arm instruction set
Compiling Draco library on Windows platform
5g NR protocol learning -- ts38.211 downlink channel
[QNX hypervisor 2.2 user manual] 4.1 method of building QNX hypervisor system