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)的更多相关文章

  1. LeetCode 344. Reverse String(反转字符串)

    题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...

  2. [LeetCode] 344. Reverse String 翻转字符串

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

  3. 344 Reverse String 反转字符串

    请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...

  4. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  5. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  6. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  7. Leetcode 344 Reverse String 字符串处理

    题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...

  8. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

  9. Python [Leetcode 344]Reverse String

    题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...

随机推荐

  1. 避免HBase PageFilter踩坑,这几点你必须要清楚

    有这样一个场景,在HBase中需要分页查询,同时根据某一列的值进行过滤. 不同于RDBMS天然支持分页查询,HBase要进行分页必须由自己实现.据我了解的,目前有两种方案, 一是<HBase权威 ...

  2. switch case加范围判断

    switch case是可以加范围判断的,但是语法有少许变化,参数不能写在switch里面,而是写在外面,如: const i = 3; switch (true) { case (i <= 0 ...

  3. 浅析Java中线程组(ThreadGroup类)

    Java中使用ThreadGroup类来代表线程组,表示一组线程的集合,可以对一批线程和线程组进行管理.可以把线程归属到某一个线程组中,线程组中可以有线程对象,也可以有线程组,组中还可以有线程,这样的 ...

  4. CMake编译的VS工程,安装时遇到错误:error MSB3073: 命令“setlocal

    错误提示 70>CMake Error at src/base/cmake_install.cmake:63 (file): 70> file INSTALL cannot find 70 ...

  5. Java基础之 集合体系结构(Collection、List、ArrayList、LinkedList、Vector)

    Java基础之 集合体系结构详细笔记(Collection.List.ArrayList.LinkedList.Vector) 集合是JavaSE的重要组成部分,其与数据结构的知识密切相联,集合体系就 ...

  6. SpringMVC 之 上传文件

    一.需求: 利用SpringMVC实现上传文件的功能 二.思路: 1.我们可以在SpringMVC中,通过配置一个MultipartResolver来上传文件. 2.通过MultipartFile f ...

  7. 汇编push,pop

    版权声明:本文为博主原创文章,转载请附上原文出处链接和本声明.2019-08-24,00:40:12作者By-----溺心与沉浮----博客园 1.BASE,TOP是2个32位的通用寄存器,里面存储的 ...

  8. 剑指:和为S的连续正数序列

    题目描述 输入一个正数 s,打印出所有和为 s 的连续正数序列(至少含有两个数). 例如输入 15,由于 1+2+3+4+5=4+5+6=7+8=15,所以结果打印出 3 个连续序列 1-5.4-6 ...

  9. foreachRDD

    需求: 将统计结果写入到MySQL create table wordcount( word varchar(50) default null, wordcount int(10) default n ...

  10. IEDA创建Springboot项目

    随着技术的更新对于开发速度的追求,我们越来越不能忍受的是Spring框架对于集成开发以后大量的配置问题.所以SprigBoot应运而生,SpringBoot框架其实就是在Spring框架的外边包裹上了 ...