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. SPOJ - BALNUM - Balanced Numbers(数位DP)

    链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...

  2. Kafka、ActiveMQ、RabbitMQ、RocketMQ区别

    1.区别: Kafka和RocketMQ的区别: 1.两者对于消息的单机吞吐量.时效性.可用性.消息可靠性都差不多,其中时效性就是消息延迟都在ms级,kafka吞吐量会更大. 2.功能支持方面:Kaf ...

  3. 数据库Count 语句详解

    数据库查询相信很多人都不陌生,所有经常有人调侃程序员就是CRUD专员,这所谓的CRUD指的就是数据库的增删改查.在数据库的增删改查操作中,使用最频繁的就是查询操作.而在所有查询操作中,统计数量操作更是 ...

  4. git中常用命令

    1.全局安装git Git-2.11.1-64-bit() //配置gitgit config --global user.name "您的git账号名"git config -- ...

  5. tar归档压缩命令和zip归档 和7zip压缩命令;库文件归档ar命令

    第一.tar 归档 tar -c 创建归档文件包 tar -x 释放归档文件包 tar -t 查看归档文件包 tar -v 显示归档包操作过程信息 tar -f 指定归档文件名 案例1:归档 /hom ...

  6. 洛谷 P1901 发射站 题解

    P1901 发射站 题目描述 某地有 N 个能量发射站排成一行,每个发射站 i 都有不相同的高度 Hi,并能向两边(当 然两端的只能向一边)同时发射能量值为 Vi 的能量,并且发出的能量只被两边最近的 ...

  7. linux (core dump)调试

    转自 http://www.cnblogs.com/hazir/p/linxu_core_dump.html Linux Core Dump 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内 ...

  8. SDOI2015做题记录

    由于我懒,并且这里面除了D2T3恶心以外都不难写,所以很多代码都没写-- 排序 对于某一个合法的操作序列(操作序列定义为每次交换的两组数),可以随意交换顺序,仍然合法.所以对于一个操作集合,答案就加\ ...

  9. 一个有趣的js隐式转换的问题

    一个有趣的js隐式转换的问题 在chrome的控制台中打印一下表达式 [] + {} //结果为 [object object] 然后调整顺序打印 {} + [] //结果为 0 然后将两个表达式组合 ...

  10. [golang]写了一个可以用 go 来写脚本的工具:gosl

    转自:https://golangtc.com/t/53cca103320b52060a000030 写了一个可以用 go 来写脚本的工具:gosl 代码和使用说明可以看这里: http://gith ...