Leetcode#344. Reverse String(反转字符串)
题目描述
编写一个函数,其作用是将输入的字符串反转过来。
示例 1:
输入: "hello"
输出: "olleh"
示例 2:
输入: "A man, a plan, a canal: Panama"
输出: "amanaP :lanac a ,nalp a ,nam A"
思路
思路一:
逆序拼接字符串
思路二:
依次交换两边的值
思路三:
直接调用StringBuilder 的 reverse()
思路四:
用栈来实现反转
代码实现
package String;
import java.util.Stack;
/**
* 344. Reverse String(反转字符串)
* 编写一个函数,其作用是将输入的字符串反转过来。
*/
public class Solution344 {
public static void main(String[] args) {
Solution344 solution344 = new Solution344();
String s = "hello";
System.out.println(solution344.reverseString_4(s));
}
/**
* 逆序拼接字符串
*
* @param s
* @return
*/
public String reverseString(String s) {
StringBuilder str = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
str.append(s.charAt(i));
}
return String.valueOf(str);
}
/**
* 依次交换两边的值
*
* @param s
* @return
*/
public String reverseString_2(String s) {
char[] chars = s.toCharArray();
int i = 0;
int j = chars.length - 1;
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
return new String(chars);
}
/**
* 直接调用StringBuilder 的 reverse()
*
* @param s
* @return
*/
public String reverseString_3(String s) {
return new StringBuilder(s).reverse().toString();
}
/**
* 用栈来实现反转
*
* @param s
* @return
*/
public String reverseString_4(String s) {
Stack<Character> stack = new Stack<>();
char[] chars = s.toCharArray();
String res = "";
for (int i = 0; i < chars.length; i++) {
stack.push(chars[i]);
}
for (int i = 0; i < chars.length; i++) {
res += stack.pop();
}
return res;
}
}
Leetcode#344. Reverse String(反转字符串)的更多相关文章
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
- [LeetCode] 344. Reverse String 翻转字符串
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- 344 Reverse String 反转字符串
请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- Leetcode 344 Reverse String 字符串处理
题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...
- (字符串 数组 递归 双指针) leetcode 344. Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- LeetCode 344. Reverse String
Problem: Write a function that takes a string as input and returns the string reversed. Example: Giv ...
随机推荐
- slice()和splice()区别
1.slice(start,end):方法可从已有数组中返回选定的元素,返回一个新数组,包含从start到end(不包含该元素)的数组元素. 注意:该方法不会改变原数组,而是返回一个子数组,如果想删除 ...
- shell中的EOF用法
重定位运算符 >> 是追加内容> 是覆盖原有内容 1.EOF Shell中通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止 ...
- 实验吧 who are you
看到ip,然后提示是要把ip写到数据库里面,就想到了x-forwarded-for注入 扔burp里面试一下 确实有这个问题,从返回信息里面估计出来,应该是盲注,而且基于时间的盲注,试一下吧 测试延迟 ...
- 如何展开Linux Memory Management学习?
Linux的进程和内存是两座大山,没有翻过这两座大山对于内核的理解始终是不完整的. 关于Linux内存管理,在开始之前做些准备工作. 首先bing到了Quora的<How can one rea ...
- 四:OVS+GRE之网络节点
关于Neutron上的三种Agent的作用: Neutron-OVS-Agent:从OVS-Plugin上接收tunnel和tunnel flow的配置,驱动OVS来建立GRE Tunnel Neut ...
- iframe 自适应
<iframe src="http://www.fulibac.com" id="myiframe" scrolling="no" o ...
- 在项目中迁移MS SQLServer到Mysql数据库,实现MySQL数据库的快速整合
在开发项目的时候,往往碰到的不同的需求情况,兼容不同类型的数据库是我们项目以不变应万变的举措之一,在底层能够兼容多种数据库会使得我们开发不同类型的项目得心应手,如果配合快速的框架支持,那更是锦上添花的 ...
- easyui-tab标签
一. 加载方式 //class 加载方式<div id="box" class="easyui-tabs" style="width:500px ...
- BZOJ3328 PYXFIB 单位根反演
题意:求 \[ \sum_{i=0}^n[k|i]\binom{n}{i}Fib(i) \] 斐波那契数列有简单的矩阵上的通项公式\(Fib(n)=A^n_{1,1}\).代入得 \[ =\sum_{ ...
- 简单的JQuery完美拖拽
首先导入jq库,最好是1.0版本的.调用函数时,传入要拖拽元素的id值. function drag(sel){ $div = $("#"+sel); $div.mousedown ...