当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302

QT添加QObject类(想使用信号和槽)遇到的问题汇总,亲测解决有效error: undefined reference to `vtable for xxxxxx(你的类名)‘

ARM处理器模式与寄存器

Node crawler puppeter usage

5g NR protocol learning -- ts38.211 downlink channel

Cookie和Session
![[foundation of deep learning] learning of neural network (4)](/img/8d/0e1b5d449afa583a52857b9ec7af40.png)
[foundation of deep learning] learning of neural network (4)

Blue Bridge Cup 2015 CA provincial competition (filling the pit)

PDSCH related

6.6 分離卷積
随机推荐
Doris记录服务接口调用情况
ARP protocol data processing process of neighbor subsystem
Lambda and filter, List 和 numpy array的索引,以及各种距离指标distance-metrics,拼接数组以及axis=0 and axis=1的区分
Getting started with NVIDIA Jetson nano Developer Kit
5g NR Protocol Learning - - ts38.211 downlink channel
LeetCode_字符串_简单_344.反转字符串
视频分类的类间和类内关系——正则化
一个人必须不停地写作,才能不被茫茫人海淹没。
单元测试用例框架--unittest
B.刷墙(C语言)
Pytoch notes
One must keep writing, so as not to be submerged by the vast crowd.
标品和非标品如何选品,选品的重要性,店铺怎样布局
Load/store memory access instruction of arm instruction set (1)
Pseudo instruction of arm instruction set
NVIDIA Jetson Nano Developer Kit 入门
K52. Chapter 1: installing kubernetes v1.22 based on kubeadm -- cluster deployment
Kubernetes cluster setup
Go sends SMS based on Tencent cloud
5g NR protocol learning -- ts38.211 downlink channel