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 ...
随机推荐
- norm()函数
n = norm(v) 返回向量 v 的欧几里德范数.此范数也称为 2-范数.向量模或欧几里德长度. 例1: K>> norm([3 4]) ans = 5
- python django -在setting 设定全局时间格式
工作中遇到需要全局设定时间的格式,再此马克下 USE_L10N = False DATE_FORMAT = 'Y-m-d' DATETIME_FORMAT = 'Y年m月'
- 获取上一个页面的data
let pages = getCurrentPages();// 获取页面栈 let current = pages[pages.length - 1]; // 当前页面 let url = curr ...
- mongoDB线上数据库连接报错记录
报错信息提示:无法在第一次连接时连接到服务器 别的一切正常,经过查询得知,是因为如果电脑没设定固定IP,并且重启情况下可能会导致IP地址更改. 解决方法: 将本机ip地址添加到cluster的白名单即 ...
- git 查看项目代码统计命令
git log --author="xxxxxxxx" --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; ...
- while循环实现十进制转二进制
#include <stdio.h> int main(void){ int a,n; printf("pls input number:\n"); scanf(&qu ...
- 洛谷 P3979 遥远的国度
题目描述 修改某条路径上的值以及询问子树的最小值都最树剖的基础操作,那么如何实现换根呢? 考虑一下三种情况: 1.rot=询问的子树x,答案就是整棵树的最小值 2.rot在x的子树里,只有rot到x这 ...
- Ceph osd故障恢复
1 调高osd的日志等级 加上红框那一行就可以了 osd的日志路径:/var/log/ceph/ceph-osd.3.log 注意:加上了这一行后日志会刷很多,所以要特别注意日志容量的变化,以防把v ...
- 在Matlab终止程序后的异常
有时终止Matlab程序后,其内部指令会执行异常,出现不识别命令函数的情形.我遇到过执行sum命令出错的问题.退出程序,重启后正常.
- Python3文件
open()方法 Python open()方法永于打开一个文件,并返回文件对象,并对文件进行处理过程中都需要用到这个方法,如果该文件无法被打开,则抛出OSError 注意:使用open()方法一定要 ...