LeetCode_344. Reverse String
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 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的更多相关文章
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...
- 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 ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- 344. Reverse String(C++)
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- 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) { ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
随机推荐
- vue-cli搭建项目的坑
使用vue-cli生成的项目默认没有 --open,所以npm run dev运行项目后,不会自动打开浏览器, 需要手动添加--open,反之,如果不需要自动打开浏览器,删除就好了
- JDK、JRE、JVM之间的关系及JDK安装
JRE (Java Runtime Environment) :是Java程序的运行时环境,包含 JVM 和运行时所需要的 核心类库 .JDK (Java Development Kit):是Java ...
- grep命令选项
-c:只输出匹配行的计数. -I:不区分大 小写(只适用于单字符). -h:查询多文件时不显示文件名. -l:查询多文件时只输出包含匹配字符的文件名. -n:显示匹配行及 行号. -s:不显示不存在或 ...
- Hello 2019【A,B,C】
#include<bits/stdc++.h> using namespace std; #define int long long signed main(){ string str; ...
- HTML5新增常用标签
1.header 标签定义文档的页眉(介绍信息). <body> <article> <header> <h1>What Does WWF Do?< ...
- editplus 支持lua语言语法高亮显示
找到自己的安装目录 建一个这个名字的文件 里面写上 #TITLE=LUA ; LUA syntax file written by ES-Computing. ; This file is requi ...
- 洛谷 P1102 A-B数对 题解
P1102 A-B 数对 题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+B Problem,改用 A-B 了哈哈! 好吧,题目是这样的:给出一串数以及一个数字 ...
- 【一起来烧脑】一步学会JavaScript体系
[外链图片转存失败(img-b0GOhxRY-1563571645197)(https://upload-images.jianshu.io/upload_images/11158618-ba249b ...
- SSM + ehcache 异常
异常如下: 十二月 26, 2017 1:24:44 下午 org.apache.jasper.servlet.TldScanner scanJars 信息: At least one JAR was ...
- P1108 低价购买——最长下降子序列+方案数
P1108 低价购买 最长下降子序列不用多讲:关键是方案数: 在求出f[i]时,我们可以比较前面的f[j]; 如果f[i]==f[j]&&a[i]==a[j] 要将t[j]=0,去重: ...