当前位置:网站首页>344. Reverse string
344. Reverse string
2022-07-26 10:54:00 【Forest_ one thousand and ten】
Title Description : Write a function , Its function is to invert the input string . Input string as character array char[] Given in the form of .
Limit : Do not allocate extra space to another array , You have to modify the input array in place 、 Use O(1) To solve this problem .
Ideas : Double pointer , Point to the head and tail at the same time , Exchange two positions , And then shrink to the middle at the same time .
The method of exchanging two numbers
Method 1: Use temporary variables
C++
// Suppose the scenario is that calling this function can exchange the values of two numbers , So input parameter pointer and pass array are ok
// But you can't pass values , Such as void swap(int x,int y)
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
For clarity , The following are all ways of using value passing .
Method 2: XOR operation using bit operation , Languages that support bit operations , This method is the most efficient .
principle :a^ b ^ b=a
a = a ^ b;
b = a ^ b;
a = a ^ b;
Method 3: Using logical operations
a=a+b;
b=a-b
a=a-b;
边栏推荐
- Visual conversion of nmap vulnerability scanning results
- Drbl diskless startup + Clonezilla network backup and restore system
- Bash shell learning notes (VII)
- Bash shell学习笔记(一)
- MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常
- 面试知识点
- C#halcon用户控件崩溃的一种处理方法
- Esxi6.5 patch update
- Minesweeping Pro version 2021-08-19
- Successfully transplanted stemwin v5.22 on Shenzhou IV development board
猜你喜欢

344.反转字符串

Bash shell learning notes (III)

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2

35. Search the insertion position

Codepoint 58880 not found in font, aborting. flutter build apk时报错

Kali view IP address

RT thread learning notes (V) -- edit, download and debug programs

软件测试综述之软件测试的背景、实质、软件开发的过程

Wireshark basic tutorial Ethernet frame analysis.

Bash shell learning notes (V)
随机推荐
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
Wechat official account message notice "errCode": 40164, "errmsg": "invalid IP
二叉树的遍历 递归+迭代
2021-08-12 function recursion_ Learn C language with brother Peng
Sword finger offer (44): flip the word order sequence
232.用栈实现队列
C language pengge 20210812c language function
MFC多线程的简单使用
Sword finger offer (8): jump the steps
MySQL quick learning notes-2021-08-31
@Notblank, @notnull, @notempty differences and uses
Build ARM embedded development environment
Definition and use of C language namespace
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
mysql20210906
Error[pe147]: declaration is incompatible with 'error problem
postman 导出导入
logging 高级使用
C语言命名空间的定义与使用
Bash shell learning notes (I)