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. Mysql 根据时间取出每组数据中最新的一条

    下策——查询出结果后将时间排序后取第一条 select * from a where create_time<="2017-03-29 19:30:36"order by c ...

  2. Hive ACID和事务表支持详解

    一.ACID介绍 ACID就是常见数据库事务的四大特性:Atomicity(原子性).Consistency(一致性).Isolation(隔离性).Durability(持久性). 在Hive 0. ...

  3. 数据库访问优化之四:减少数据库服务器CPU运算

    1.使用绑定变量 绑定变量是指SQL中对变化的值采用变量参数的形式提交,而不是在SQL中直接拼写对应的值. 非绑定变量写法:Select * from employee where id=123456 ...

  4. csp-s模拟测试93T2口胡(蒟蒻的口胡大家显然就不用看了吧

    我们先证正确性,再证复杂度 以下记$\left \langle i,j \right \rangle$为考虑$\left [ i,j \right ]$的点时的最优决策 $\left \langle ...

  5. Cogs 56. 质数取石子(博弈)

    质数取石子 ★★ 输入文件:stonegame.in 输出文件:stonegame.out 简单对比 时间限制:1 s 内存限制:128 MB 问题描述 DD 和 MM 正在玩取石子游戏.他们的游戏规 ...

  6. LibreOJ #527. 「LibreOJ β Round #4」框架

    二次联通门 : LibreOJ #527. 「LibreOJ β Round #4」框架 /* LibreOJ #527. 「LibreOJ β Round #4」框架 %% xxy dalao 对于 ...

  7. 洛谷P1901发射站

    题目 一道单调栈裸题,主要是用单调栈维护单调性,和单调队列都可以在\(O(n)\)的时间内得出单调最大值或最小值,要比堆要快. 这个题可以用h来当做单调栈的使用对象,即用单调栈来维护高度,高度是越在栈 ...

  8. FLUENT求解传热系数surfaceheattransfercoef.的参考值的设置【转载】

    转载自:http://blog.sina.com.cn/s/blog_7ef78d170101ch30.html surface heat transfer coef. 计算公式: FLUENT求解传 ...

  9. Pycon 北京2019

  10. 13.mysql数据库

    1.mysql数据库建立           yum install mysql-server           mysql -u root                  mysqladmin ...