344. Reverse String

Easy

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"]
package leetcode.easy;

public class ReverseString {
private static void print_arr(char[] s) {
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
System.out.println();
} public void helper(char[] s, int left, int right) {
if (left >= right) {
return;
}
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
helper(s, left, right);
} public void reverseString1(char[] s) {
helper(s, 0, s.length - 1);
} public void reverseString2(char[] s) {
int left = 0, right = s.length - 1;
while (left < right) {
char tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
} @org.junit.Test
public void test1() {
char[] s1 = { 'h', 'e', 'l', 'l', 'o' };
char[] s2 = { 'H', 'a', 'n', 'n', 'a', 'h' };
print_arr(s1);
reverseString1(s1);
print_arr(s1);
print_arr(s2);
reverseString1(s2);
print_arr(s2);
} @org.junit.Test
public void test2() {
char[] s1 = { 'h', 'e', 'l', 'l', 'o' };
char[] s2 = { 'H', 'a', 'n', 'n', 'a', 'h' };
print_arr(s1);
reverseString1(s1);
print_arr(s1);
print_arr(s2);
reverseString1(s2);
print_arr(s2);
}
}

LeetCode_344. Reverse String的更多相关文章

  1. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  2. LeetCode Reverse String

    原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...

  3. Nim Game,Reverse String,Sum of Two Integers

    下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...

  4. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  5. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

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

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

  7. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  8. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

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

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

随机推荐

  1. 洛谷 P1032 字串变换 题解

    每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...

  2. python 使用 jt400.jar

    jt400helper.py #coding=utf-8 import jpype import os class JT400Helper(object): def __init__(self, se ...

  3. mysqli扩展和持久化连接

    mysqli扩展的持久化连接在PHP5.3中被引入.支持已经存在于PDO MYSQL 和ext/mysql中. 持久化连接背后的思想是客户端进程和数据库之间的连接可以通过一个客户端进程来保持重用, 而 ...

  4. VIJOS PID221 / 烦人的幻灯片

     暴力出奇迹,学长诚不欺我. PID221 / 烦人的幻灯片 2017-04-14 19:47:08 运行耗时:30 ms 运行内存:12292 KB 查看最后一次评测记录 题目描述 李教授于今天下午 ...

  5. Ceph osd故障恢复

    1  调高osd的日志等级 加上红框那一行就可以了 osd的日志路径:/var/log/ceph/ceph-osd.3.log 注意:加上了这一行后日志会刷很多,所以要特别注意日志容量的变化,以防把v ...

  6. git 导出远程特定分之

    很多时候,git clone 只是 clone 下来了 master 分支,如果想 clone 特定分支.有的时候不知如何是好. 找到了如下的命令,记录一下.以便有需要的同学可以使用. git co ...

  7. [php]Windows环境下Composer的安装教程

    方法一: 下载Composer-Setup.exe后安装,它会自动搜索 php.exe 路径, 如果找不到,则手动添加路径.   Windows安装Composer 方法二: 如果出现如下错误,说明伟 ...

  8. C# 百度API地址坐标互相转换

    通过C#代码将地址字符串转为经纬度坐标,或者将经纬度转为具体的地址字符串,在不通外网的项目中是有需求的. 具体步骤: 一.创建BaiduMapHelper,用于定义地址信息和请求. public st ...

  9. SpringBoot获取Freemarker模板引擎,生成HTML代码

    今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...

  10. Linux下的crontab定时执行任务命令

    0x00 简介 在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间. cron的 ...