leetcode_344
来源:互联网 发布:ae pr是什么软件 编辑:程序博客网 时间:2024/06/05 04:59
1题目:给一个字符串,反向输出:
例如:“hello”,输出“olleh”
2代码:
package com.leetcode;
import java.util.Scanner;
import java.util.Stack;
public class C344 {
public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("please input String:"); String str = in.next(); System.out.println(reverse6(str));}public static String reverse1(String s) { int length = s.length(); if(length <= 1) { return s; } String left = s.substring(0,length/2); String right = s.substring(length/2,length); return reverse1(right) + reverse1(left);}public static String reverse2(String s) { char[] chars = new char[s.length()]; int index = 0; for(int i = s.length() - 1;i>=0;i--) { chars[index++] = s.charAt(i); } return new String(chars);}public static String reverse3(String s) { String reverse = ""; for(int i = s.length() -1;i>=0;i--) { reverse += s.charAt(i); } return reverse;}public static String reverse4(String s) { return new StringBuffer(s).reverse().toString();}public static String reverse5(String s) { char[] chars = s.toCharArray(); int n = chars.length-1; for(int i=0;i<=n/2;i++) { char tem = chars[i]; chars[i] = chars[n-i]; chars[n-i] = tem; } return new String(chars);}public static String reverse6(String s) { char[] chars = s.toCharArray(); int begin = 0; int end = chars.length - 1; while (begin < end ) { chars[begin] = (char) (chars[begin] ^ chars[end]); chars[end] = (char) (chars[begin] ^ chars[end]); chars[begin] = (char) (chars[begin] ^ chars[end]); begin++; end--; } return new String(chars);}public static String reverse7(String s){ char[] chars = s.toCharArray(); Stack<Character> stack = new Stack<>(); String reverse = ""; for(int i = 0;i<chars.length;i++) { stack.push(chars[i]); } for(int i = 0;i<chars.length;i++) { reverse += stack.pop(); } return reverse;}
}
3分析: java解决这个问题方法很容易想到多种,以上就是我列出来的7种方法。
但大体可以分为三类:利用java已有的封装好的方法、利用交换、利用堆栈。
(1)利用java已有的封装好的方法
public static String reverse1(String s) {//用二分法,递归
int length = s.length();
if(length <= 1) {
return s;
}
String left = s.substring(0,length/2);
String right = s.substring(length/2,length);
return reverse1(right) + reverse1(left);}public static String reverse2(String s) { char[] chars = new char[s.length()]; int index = 0; for(int i = s.length() - 1;i>=0;i--) { chars[index++] = s.charAt(i); } return new String(chars);}public static String reverse3(String s) { String reverse = ""; for(int i = s.length() -1;i>=0;i--) { reverse += s.charAt(i); } return reverse;}public static String reverse4(String s) { return new StringBuffer(s).reverse().toString();}
(2)利用交换
public static String reverse5(String s) {
char[] chars = s.toCharArray();
int n = chars.length-1;
for(int i=0;i<=n/2;i++) { char tem = chars[i]; chars[i] = chars[n-i]; chars[n-i] = tem; } return new String(chars);}public static String reverse6(String s) {//效率会高一点,不费空间实现交换 char[] chars = s.toCharArray(); int begin = 0; int end = chars.length - 1; while (begin < end ) { chars[begin] = (char) (chars[begin] ^ chars[end]); chars[end] = (char) (chars[begin] ^ chars[end]); chars[begin] = (char) (chars[begin] ^ chars[end]); begin++; end--; } return new String(chars);}
(3)利用堆栈
public static String reverse7(String s){ char[] chars = s.toCharArray(); Stack<Character> stack = new Stack<>(); String reverse = ""; for(int i = 0;i<chars.length;i++) { stack.push(chars[i]); } for(int i = 0;i<chars.length;i++) { reverse += stack.pop(); } return reverse; }
1 0
- leetcode_344
- leetcode_344 Reverse String
- LeetCode_344. Reverse String
- android使用全局变量的两种方法
- 深入研究分析java.lang.Class类
- Android Notification通知详解
- STM32学习---GPIO
- java 1如何将字串 String 转换成整数 int?
- leetcode_344
- wireworld
- 一例简单的LaTex书籍模板
- PHP内核--命名空间
- 欢迎使用CSDN-markdown编辑器
- EasyUI之选项卡Tabs
- 面向对象的三个基本元素,五个基本原则
- struts学习第一记
- POJ 1308