Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

344.反转字符串 #5

Open
weycen opened this issue Oct 17, 2021 · 0 comments
Open

344.反转字符串 #5

weycen opened this issue Oct 17, 2021 · 0 comments
Labels
📚01-数组和字符串 leetbook 📝简单 LeetCode题目难度

Comments

@weycen
Copy link
Owner

weycen commented Oct 17, 2021

🚀 题解

题目描述

题目名称:344. 反转字符串

编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。

解题参考

官方题解-反转字符串

思路及代码

方法一:双指针

思路

  1. 将 left 指向字符数组首元素,right 指向字符数组尾元素。
  2. 当 left < right:
  • 交换 s[left] 和 s[right];
  • left 指针右移一位,即 left = left + 1;
  • right 指针左移一位,即 right = right - 1。
  1. 当 left >= right,反转结束,返回字符数组即可。

代码

344.反转字符串.c

还有要说的?

LeetCode刷的第10道题!从今年2月份到现在,8个月了,真不容易,继续努力吧...

@weycen weycen added the 📝简单 LeetCode题目难度 label Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚01-数组和字符串 leetbook 📝简单 LeetCode题目难度
Projects
None yet
Development

No branches or pull requests

1 participant