[leetcode]344. Reverse String

2023-05-16

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.

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"]

Solution:

class Solution {
    public void reverseString(char[] s) {
        if(s == null) {
            return;
        }
        int len = s.length;
        for(int i = 0; i < len/2; i ++) { 
            swap(s,i,len-1-i);
              
        }
    }
    public void swap(char[] s, int i, int j) {
        char tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[leetcode]344. Reverse String 的相关文章

  • C++/STL 字符串:如何使用通配符模仿正则表达式之类的函数?

    我想使用通配符比较 4 个字符串 例如 std string wildcards H RH H 0 5 in the last one I need to check if string is H0 and H5 只用STL能实现吗 谢谢
  • 找出段落中出现的单词

    sentence Alice was not a bit hurt and she jumped up on to her feet in a moment words Alice jumped played 我可以使用filterpyth
  • 反转默认比例梯度ggplot2

    我是新手 我正在尝试设计热图 这是我的代码 ggplot gd aes Qcountry Q6 1 Q6d order TRUE geom tile aes fill prob colour white theme minimal labs
  • Bash 字符串之间的比较 - 相等但不相等

    我只想在 Bash 中的两个字符串之间进行非常简单的比较 stat curl Is url head n 1 echo stat if stat HTTP 1 1 200 OK then echo symbol is OK echo sta
  • Pandas - 修改每个单元格中的字符串值

    我有一个 pandas 数据框 我需要修改给定字符串列中的所有值 每列包含相同长度的字符串值 用户提供他们想要为每个值替换的索引 例如 1 3 和重置价值 AAA 这会将值 1 到 3 中的字符串替换为值AAA 我怎样才能使用applyma
  • 需要Python字长函数示例

    我的家庭作业有点困难 我本来应该编写一个函数 limitWords 将输入限制为 20 个单词 如果输入超过 20 个单词 则将输入截断为仅 20 个单词 我使用 len text split 作为计算单词的方法 因此 20 个或更少的部分
  • C# 模式匹配

    我对 C 有点陌生 我正在寻找一个字符串匹配模式来执行以下操作 我有一个像这样的字符串 该书将在 唐宁街 11 号接待处 并将由主要医疗保健人员参加 我需要创建一个 span 标签来使用 startIndex 和 length 突出显示一些
  • 通过字符串操作预防 PHP SQL 注入[重复]

    这个问题在这里已经有答案了 可能的重复 PHP 中防止 SQL 注入的最佳方法 https stackoverflow com questions 60174 best way to prevent sql injection in php
  • 将字符串中的 i 个连续相同字符分组到列表中[重复]

    这个问题在这里已经有答案了 我希望以这样的方式分隔输入字符串 即所有连续的相同字符都分组在一个列表中 示例1 str aabbcccdeddgg output aa bb ccc d e dd 期望的输出 aa bb ccc d e dd
  • 为什么使用 string::iterator 而不是索引? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么使用迭代器而不是数组索引 https stackoverflow com questions 131241 why use iterators instead of array indices
  • SQL 查询结果为字符串(或变量)

    是否可以将SQL查询结果输出到一个字符串或变量中 我的php和mysql不好 假设我有数据库 agents 其中包含列 agent id agent fname agent lname agent dept 使用此查询 sql SELECT
  • Python从int到string的快速转换

    我正在用 python 求解大量阶乘 并发现当我完成计算阶乘时 需要相同的时间才能转换为字符串以保存到文件中 我试图找到一种将 int 转换为字符串的快速方法 我将举一个计算和 int 转换时间的例子 我正在使用通用的 a str a 但感
  • Java在字符串中看不到空格[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 因此 我正在尝试解析一些具有多行文本的文本文件 我的工作是检查所有单词并将其打印在文件中 因此 我读取了所有行 循环遍历它们并用空格分隔每
  • AsyncTask:DoInBackground(String ...)与DoInBackground(Params ...)冲突?

    当尝试使用时Async task要执行 HTTP post 我得到以下信息 ASyncTask DoInBackground String clashes with DoInBackground Params in Android os A
  • Python3,当 UserString 不表现为字符串时?

    在Python3中我使用UserString扩展内置字符串的功能 通常UserStrings 的行为就像strs 但与re我遇到了意想不到的情况TypeError bpython version 0 17 1 on top of Pytho
  • MySql 查询在选择中将 NULL 替换为空字符串

    如何用空字符串替换 select 中的 NULL 值 输出 NULL 值看起来不太专业 这是非常不寻常的 根据我的语法 我希望它能够工作 我希望能得到一个解释 为什么没有 select CASE prereq WHEN prereq IS
  • Java:从 StringBuilder 中删除字符串[重复]

    这个问题在这里已经有答案了 我想从 StringBuilder 中删除 String Example String aaa sample String bbb sample2 String ccc sample3 在另一部分 StringB
  • java的split string方法也可以返回带分隔符的数组

    当我们使用String Split java中对字符串进行分割的方法 其工作原理如下 String s hello my dear String ss s split 数组ss包含 Hello my dear 但在这种情况下 空格 即分隔符
  • SWI Prolog 转义引号

    我需要在序言中将 放在字符串周围 我从另一个程序获取输入 看起来我无法转义该程序中的 因此我必须在序言中添加 否则序言语句将不起作用 感谢您的帮助 为了讨论strings https stackoverflow com a 39922411
  • 如何连接字符串和常量字符?

    我需要将 hello world 放入c中 我怎样才能做到这一点 string a hello const char b world const char C string a hello const char b world a b co

随机推荐

  • Hashmap1.7和1.8区别+ConcurrentHashmap1.7和1.8区别

    Hashmap JDK1 7中 使用一个Entry数组来存储数据 xff0c 用key的hashcode取模来决定key会被放到数组里的位置 xff0c 如果hashcode相同 xff0c 或者hashcode取模后的结果相同 xff0c
  • TOP k问题

    题目 xff1a 有1千万条短信 xff0c 有重复 xff0c 以文本文件的形式保存 xff0c 一行一条 xff0c 有重复 请用5分钟时间 xff0c 找出重复出现最多的前10条 解析 xff1a 对于本题来说 xff0c 某些面试者
  • CAS操作是怎么实现的

    CAS是compare and swap xff0c 翻译过来就是比较并交换 维护三个变量值 xff0c 一个是内存值V xff0c 一个是期望的旧的值A xff0c 一个是要更新的值B 更新一个变量的时候 xff0c 只有当预期值A与内存
  • liunx在整合springboot 项目时出现错误CLUSTERDOWN The cluster is down

    在运行springboot项目并且把项目利用二级缓存 xff0c 储存到集群中时候在idea下报错显示CLUSTERDOWN The cluster is down 可以进入linux中查看集群配置 连接到某个节点此时显示此时节点明显显示的
  • volatile关键字

    java提供了一种稍弱的同步机制 xff0c volatile变量 xff0c 用来确保将变量的更新操作通知到其他线程 当把变量声明为volatile类型的时候 xff0c 编译器与运行时都会注意到这个变量是共享的 xff0c 所以不会将该
  • MVCC

    在并发读写数据库时 xff0c 读操作可能会不一致的数据 xff08 脏读 xff09 为了避免这种情况 xff0c 需要实现数据库的并发访问控制 xff0c 最简单的方式就是加锁访问 由于加锁会将读写操作串行化 xff0c 所以不会出现不
  • AQS理解

    AbstractQueuedSynchronizer简称AQS xff0c 是一个用于构建锁和同步容器的框架 事实上concurrent包内许多类都是基于AQS构建 xff0c 例如ReentrantLock Semaphere Count
  • B树、B+树及索引

    B树 xff1a 每个节点都存储key和data xff0c 所有节点组成这棵树 xff0c 并且叶子节点指针为null B 43 树 xff1a 只有叶子节点存储data xff0c 叶子节点包含了这棵树的所有键值 xff0c 叶子节点不
  • Arrays.sort和Collections.sort实现原理解析

    Collections sort方法底层就是调用的Arrays sort方法 写一个例子看源码 xff1a public static void main String args List lt String gt strings 61 A
  • Java代理模式之动态代理

    代理模式是设计模式中非常重要的一种类型 代理模式从类型上来说 xff0c 可以分为静态代理和动态代理两种类型 假设一个场景 xff0c 有一个蛋糕店 xff0c 卖的蛋糕都是用蛋糕机做的 xff0c 而且不同种类的蛋糕由不同的蛋糕机来做 x
  • 二叉树镜像

    求二叉树镜像 public class Solution public void Mirror TreeNode root if root 61 61 null return if root left 61 61 null amp amp
  • 设计模式之适配器模式

    适配器模式是作为两个不兼容的接口之间的桥梁 这种类型的设计模式属于结构型模式 xff0c 它结合了两个独立接口的功能 这种模式涉及到一个单一的类 xff0c 该类负责加入独立的或不兼容的接口功能 举个真实的例子 xff0c 读卡器是作为内存
  • 设计模式之单例模式

    1 懒汉式 xff0c 线程不安全 public class Demo1 private static Demo1 instance private Demo1 public static Demo1 getInstance if inst
  • Lambda表达式详解

    Java 8最值得学习的特性就是Lambda表达式 Lambda写的好可以极大减少代码冗余 xff0c 同时可读性也好过冗长的内部类 xff0c 匿名类 举例说明一下 xff1a xff08 1 xff09 创建线程传统写法 xff1a T
  • Android8.0以上实现APP(应用)开机自启动

    一 程序中实现APP开机自启动 可参考 xff1a https www cnblogs com jetereting p 4572302 html 二 设置APP开机自启动权限 小米手机设置开机启动应用权限 xff08 Android9 0
  • 跳台阶问题

    1 输出斐波那契数列的第n项 直接上代码 xff1a public class Fibonacci public static int fibonacci int n if n 61 61 0 return 0 if n 61 61 1 r
  • 字符串编辑距离

    字符串的编辑距离 xff0c 又称为Levenshtein距离 是利用字符操作 xff0c 把字符串A转换成字符串B所需要的最少操作数 其中 xff0c 字符操作包括 xff1a 删除一个字符插入一个字符修改一个字符 例如对于 34 hel
  • [leetcode]807.Max Increase to Keep City Skyline

    In a 2 dimensional array grid each value grid i j represents the height of a building located there We are allowed to in
  • [leetcode]1038. Binary Search Tree to Greater Sum Tree

    Given the root of a binary search tree with distinct values modify it so that every node has a new value equal to the su
  • [leetcode]344. Reverse String

    Write a function that reverses a string The input string is given as an array of characters char Do not allocate extra s