智慧有限,没想出什么新意来,出去玩了一天,活跃下脑子. public static void reverse(int[] a, int left, int right) { if (left >= right) return; int temp=a[left]; a[left]=a[right]; a[right]=temp; reverse(a,++left,--right); }
package houlei.test;import java.util.Arrays;/** * 该类创建于 2008-10-1 下午08:41:40 * * @version 1.0.0 * @author 侯磊 */ public class T { public static void reverse(int[] a, int left, int right) { // 代码写在这里 if(left>=right)return; reverse(a,left+1,right-1); int t=a[left]; a[left]=a[right]; a[right]=t; } public static void main(String args[]) throws Exception { int[] a = { 1, 2, 3, 4, 5 }; reverse(a, 0, a.length - 1); System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] }}
import java.util.Arrays;/** * 该类创建于 2008-10-1 下午08:41:40 * * @version 1.0.0 * @author 侯磊 */ public class T { public static void reverse(int[] a, int left, int right) { // 代码写在这里 if(left>=right)return; reverse(a,left+1,right-1); a[left]=a[left]+a[right]; a[right]=a[left]-a[right]; a[left]=a[left]-a[right]; } public static void main(String args[]) throws Exception { int[] a = { 1, 2, 3, 4, 5 }; reverse(a, 0, a.length - 1); System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] }}
package com;import java.util.Arrays; class T { public static void reverse(int[] a, int left, int right) { if(right-left<2) return; int temp = a[left]; a[left]=a[right]; a[right]=temp; reverse(a, ++left,--right); } public static void main(String args[]) throws Exception { int[] a = { 1, 2, 3, 4, 5 }; reverse(a, 0, a.length - 1); System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1] } }
简单的C++代码...void reverse(char* str, int len) { if (len-1<=0) return ; char c = str[0]; str[0] = str[len-1]; str[len-1] = c; reverse(str+1, len-2); }
前面有人说 的很好,我换个花样,改了个参数,更简单; package com.hust.test;import java.util.Arrays;class T {
public static void reverse(int[] a,int[] b, int left, int right) { int size=a.length; if(left==size) return; else { reverse(a,b,left+1,right-1); b[right]=a[left]; }
public class ReverseString {
public static void reverseString (String str){
if (str.length() == 1){
System.out.print(str);
}
else{
String subString1 = str.substring(0, str.length()-1);
String subString2 = str.substring(str.length()-1);
System.out.print(subString2);
reverseString (subString1);
}
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog("Please input a String: ");
reverseString (originalString);
}
}
Hi, Mate.I didn't comment on your program. I was saying that this problem is a good one and students can learn a lot from it.:-)
private static StringBuilder builder = new StringBuilder();
public static void reserse(int[] a, int left, int right) {
if(right == 0){
builder.append(a[0]);
return;
}else {
builder.append(a[right]);
reserse(a, left, right - 1);
} } public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reserse(a, 0, a.length - 1);
System.out.println(builder);
}
}
private static StringBuilder builder = new StringBuilder();
public static void reserse(int[] a, int left, int right) {
int t = 0;
t = a[right];
a[right] = a[left];
a[left] = t;
if(a.length / 2 == left + 1 || a.length / 2 == right - 1){
return;
}
reserse(a, left + 1, right - 1); } public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reserse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
// 代码写在这里
if (left == right) { //数组长度为奇数
return;
}
if (left + 1 == right) { //数组长度为偶数
int temp = a[left];
a[left] = a[right];
a[right] = temp;
} else {
int temp = a[left];
a[left] = a[right];
a[right] = temp;
reverse(a, left + 1, right - 1);
}
}
class T { public static void reverse(int[] a, int left, int right) {
if (left < right) {
int tmp = a[left];
a[left] = a[right];
a[right] = tmp;
reverse(a, ++left, --right);
}
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
public static void reserse(int[] a, int left, int right) {
int length = a.length;
exchangeValue(a, left, right);
if (length % 2 == 0 && length / 2 == left + 1) {
return;
} else if (a.length / 2 == left + 1) {
return;
}
reserse(a, left + 1, right - 1); } private static void exchangeValue(int[] a, int left, int right) {
int t = 0;
t = a[right];
a[right] = a[left];
a[left] = t;
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5, 6 };
reserse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
{ public static void reverse(int[] a, int left, int right)
{
while (left < right)
{
a[left] = a[left] + a[right];
a[right] = a[left] - a[right];
a[left] = a[left] - a[right];
left++;
right--;
} } public static void main(String args[]) throws Exception
{
int[] a =
{ 1, 2, 3, 4, 5, 6 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
return;
}这为什么能说明 长度为奇数?
{1,2,3,4,5,6}
数组长度为6left取1,right取1也不足以证明是奇数啊?
如果是奇数的话,从最后和最前开始两两比较的话,最后肯定只剩下中间一个,所以应该有right==left
如果是偶数的话,处理完两个之后剩下的还是偶数个,所以最后肯定是相邻的两个,应该有left+1==right
// 代码写在这里
if(left>=right) return;
tmp = a[left];
a[left] = a[right];
a[right] = temp;
left++;
right--;
reverse(a,left,right);
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
public static void reverse(int[] a, int left, int right) {
// 代码写在这里
if(left>=right) return; a[left] = a[left]^a[right];
a[right] = a[left]^a[right];
a[left] = a[left]^a[right];
left++;
right--;
reverse(a,left,right);
}
public class Test { /**
* @param args
*/ public static void reverse(int[] a, int left, int right) {
// 代码写在这里
if (left < right) {
a[left] += a[right];
a[right] = a[left] - a[right];
a[left] = a[left] - a[right];
reverse(a, left + 1, right -1);
} else {
return;
}
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}}
不可以,假如此数组长度是个偶数,从left=0 right=a.length-1 每次left++,right--,那么left永远不会==right的
// 代码写在这里
if((left+1)>right){//单复数都可以. 已经试验过到6了.
return;
}else{
a[left]=a[left]+a[right];
a[right]=a[left]-a[right];
a[left]=a[left]-a[right];
left++;
right--;
}
reverse(a, left, right);
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 ,6};
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
{
while(left < right)
{
int t = a[left];
a[left]=a[right];
a[right]=t;
left++;
right--;
}
}
作加法的是数组的元素,数组里有个比较大的元素不奇怪吧。单纯考虑lz的例子的话,就是1,2,3,4,5作为输入,什么都不考虑也没问题的。
if (left >= right)
return;
int temp=a[left];
a[left]=a[right];
a[right]=temp;
reverse(a,++left,--right);
}
* 该类创建于 2008-10-1 下午08:41:40
*
* @version 1.0.0
* @author 侯磊
*/
public class T {
public static void reverse(int[] a, int left, int right) {
// 代码写在这里
if(left>=right)return;
reverse(a,left+1,right-1);
int t=a[left];
a[left]=a[right];
a[right]=t;
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}}
* 该类创建于 2008-10-1 下午08:41:40
*
* @version 1.0.0
* @author 侯磊
*/
public class T {
public static void reverse(int[] a, int left, int right) {
// 代码写在这里
if(left>=right)return;
reverse(a,left+1,right-1);
a[left]=a[left]+a[right];
a[right]=a[left]-a[right];
a[left]=a[left]-a[right];
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}}
class T {
public static void reverse(int[] a, int left, int right) {
if(right-left<2)
return;
int temp = a[left];
a[left]=a[right];
a[right]=temp;
reverse(a, ++left,--right);
} public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4, 5 };
reverse(a, 0, a.length - 1);
System.out.println(Arrays.toString(a)); // 得到 [5, 4, 3, 2, 1]
}
}
{
if (len-1<=0)
return ; char c = str[0];
str[0] = str[len-1];
str[len-1] = c; reverse(str+1, len-2);
}
package com.hust.test;import java.util.Arrays;class T {
public static void reverse(int[] a,int[] b, int left, int right) {
int size=a.length;
if(left==size) return;
else
{
reverse(a,b,left+1,right-1);
b[right]=a[left];
}
}
public static void main(String args[]) throws Exception {
int[] a = { 1, 2, 3, 4};
int[] b=new int[a.length];//这是我加的一个参数
reverse(a, b, 0,a.length - 1);
System.out.println(Arrays.toString(b)); // 得到 [5, 4, 3, 2, 1]
}
}
void reverse(char * str,int left,int right)
{
if(left<right)
{
char c=str[left];
str[left]=str[right];
str[right]=c;
reverse(str,left++,right--)
}
}
用C++同样可以用二分法
void reverse(char * str,int left,int right)
{
if(left<right)
{
char c=str[left];
str[left]=str[right];
str[right]=c;
reverse(str,++left,--right);
}
}
粗心了
#include <cstdlib>
#include <iostream>
using namespace std;
void reverse(char *str,int left,int right)
{
if(left<right)
{
char c=str[left];
str[left]=str[right];
str[right]=c;
reverse(str,++left,--right);
}
};
int main(int argc, char *argv[])
{
char str[]={'1','2','3','4','5','6','7'};
cout<<sizeof(str)<<endl;
reverse(str,0,sizeof(str)-1);
cout<<str<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
不知道为什么,上段代码发生溢出了 7654321舜€|?"
#include <cstdlib>
#include <iostream>
using namespace std;
void reverse(char *str,int left,int right)
{
if(left<right)
{
char c=str[left];
str[left]=str[right];
str[right]=c;
reverse(str,++left,--right);
}
};
int main(int argc, char *argv[])
{
char str[]="1234567";
cout<<sizeof(str)<<endl;
reverse(str,0,sizeof(str)-1);
str[sizeof(str)]='\0';
cout<<str<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
更正我的错误