当前位置:网站首页>String -- 344. Reverse string
String -- 344. Reverse string
2022-07-24 11:29:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Reverse string
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 .
2 Title Example
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”]
3 Topic tips
1 <= s.length <= 105
s[i] All are ASCII Printable characters in code table
4 Ideas
For length is N The array of characters to be reversed , We can observe the change of subscript before and after inversion , Suppose the array of characters before inversion is s[o] s[1] s[2] … sLN- 1], So the inverted character array is s[N - 1] s[N - 2] …s[o]. Comparing the change of subscript before and after inversion, it is easy to get s[i] The characters of and s[N - 1 - i] There is a law of character exchange , So we can get the following solution of double pointer :
- take 1eft Point to the first element of the character array ,right Points to the end element of the character array .
- When 1eft < right :
- In exchange for s[1eft] and s[right] ;
- left Move the pointer one bit to the right , namely 1eft = left + 1 ;
- right Move the pointer one bit to the left , namely right = right - 1.
- When 1eft >= right , flip end , Return the character array .

Complexity analysis
Time complexity :O(N)O(N), among NN Is the length of the character array . All executed N/2N/2 Exchange of times .
Spatial complexity :O(1)O(1). Only constant space is used to store several variables .
5 My answer
class Solution {
public void reverseString(char[] s) {
int n = s.length;
for (int left = 0, right = n - 1; left < right; ++left, --right) {
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
}
}
}
边栏推荐
- What is cloud native? Why is cloud native technology so popular?
- Operational amplifier - Notes on rapid recovery [II] (application)
- 【Golang】golang实现简单memcache
- Leetcode 112. 路径总和
- [golang] golang implements the URLEncode URLDecode function
- Shell script
- [golang] golang实现截取字符串函数SubStr
- Ask n! How many zeros are there behind
- Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
- Self taught software testing talent -- not covered
猜你喜欢

视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?

【反序列化漏洞-01】序列化与反序列化简介

HDU5667 Sequence

Hcip OSPF interface network type experiment day 4

Robot framework official tutorial (I) getting started
![[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)

2022,软测人的平均薪资,看完我瞬间凉了...

What is the charm of CSDN members? What's the use of him?

HCIP MGRE实验 第三天

Ask n! How many zeros are there behind
随机推荐
Blue Bridge Cup provincial match training camp - Calculation of date
Sentinel vs Hystrix 限流对比,到底怎么选?
[QNX Hypervisor 2.2用户手册]9.2 cmdline
Win10 icon turns white, recovery method
哈希——242.有效的字母异位词
08【AIO编程】
高频笔试题(蔚来)
[golang] golang implements sha256 encryption function
Simply use MySQL index
[deserialization vulnerability-01] Introduction to serialization and deserialization
JPS has no namenode and datanode reasons
MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
字符串——344.反转字符串
About [software testing - interview skills and precautions for automated testing] - talk freely
The difference between YPbPr and YCbCr
07 [use of path and files classes]
网络爬虫之短信验证
High speed ADC test experience
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
FastCGI运行原理及php-fpm参数配置