Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串
公众号:爱写bug
Write a function that reverses a string. The input string is given as an array of characters char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[]
的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
Example 1:
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
解题思路:
第一个字符与最后一个交换位置,继而第二个与倒数第二个交换位置,一直交换到到中位数 结束。
代码:
Java:
class Solution {
public void reverseString(char[] s) {
char temp;
for(int i=0,j=s.length-1;i<j;i++,j--){
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
Python3:
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
i = 0
j = len(s) - 1
while (i < j):
s[i], s[j] = s[j], s[i]#交换赋值
i+=1
j-=1
其实py3有很多好玩的操作,比如这道题可以这样:s=list(reversed(s))
因为 reversed()
函数返回的是一个迭代器,所以要用 list()
函数才行。但是速度不快。
如果是字符串反转而不是数组还可以这样 s=s[::-1]
(字符串切片:string[start:stop:step]
)
总结:
这道题应当解释双指针问题最常引用的题目了,其思想是将第一个元素与末尾进行交换,再向前移动到下一个元素,并不断地交换,直到它到达中间位置。
我们可以同时使用两个指针来完成迭代:一个从第一个元素开始
,另一个从最后一个元素开始
。持续交换它们所指向的元素,直到这两个指针相遇。
摘自Leetcode:
总之,使用双指针技巧的典型场景之一是你想要
从两端向中间迭代数组。
这时你可以使用双指针技巧:
一个指针从始端开始,而另一个指针从末端开始。
Leetcode 344:Reverse String 反转字符串(python、java)的更多相关文章
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
- [LeetCode] 344. Reverse String 翻转字符串
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- 344 Reverse String 反转字符串
请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- Leetcode 344 Reverse String 字符串处理
题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...
- 【LeetCode】344. Reverse String 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...
- Python [Leetcode 344]Reverse String
题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...
随机推荐
- 面试题深入解析:Synchronized底层实现
本文为synchronized系列第二篇.主要内容为分析偏向锁的实现. 偏向锁的诞生背景和基本原理在上文中已经讲过了,强烈建议在有看过上篇文章的基础下阅读本文. 本文将分为几块内容: 1.偏向锁的入口 ...
- eclipse启动tomcat警告 [SetPropertiesRule]{Server/Service/Engine/Host/Context}
解决问题:解决办法是:关闭tomcat,双击eclipse下tomcat服务器,在出来的Tomcat server at localhost页面中找到server options选项,选中其中的选项” ...
- HighChat动态绑定数据 数据后台绑定(四)
后台绑定数据,直接返回json数据 IList<SummaryHour> adHourData = summarybll.FindList(str); List<, , , , , ...
- https://www.cnblogs.com/kxm87/p/9268622.html
数据库使用MySQL,ORM使用spring data jpa 1 因此需要再pom.xml文件中添加相应jar包.如下: <!-- 引入jap --> <dependency> ...
- Hystrix核心熔断器
在深入研究熔断器之前,我们需要先看一下Hystrix的几个重要的默认配置,这几个配置在HystrixCommandProperties 中 //时间窗(ms) static final Integer ...
- vue实现word,pdf文件的导出功能
vue实现word或pdf文档导出的功能,我的项目是:后端返回一个文档流(下图),然后前端对文档流做处理进行下载,代码如下: import axios from 'axios'; axios.get( ...
- 大白话说GIT常用操作,常用指令git操作大全
列一下在开发中用的比较多的git指令 git clone https://github.com/chineseLiao/Small-career // 克隆远程仓库到本地 git add . // 把 ...
- E203 bypass buffer
如果fifo中没有数据,且有输入,则是bypass fifo,同周期内直接把输入数据转到输出数据.如果fifo中有数据,则读取fifo,成为普通的同步fifo. module sirv_gnrl_by ...
- lua脚本之钉钉免密登录
nginx.conf worker_processes ; error_log logs/error.log; events { worker_connections ; } http { resol ...
- openstack Train 版本dashaboard 404问题
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明本文链接:https://blog.csdn.net/weixin_28738845/articl ...