题目一:找出数组中重复的数字。
具体描述:省略
package com.java.offer.one;
import java.util.InputMismatchException;
import java.util.Scanner;
//我这种方法没有书中的方法好,因为虽然时间复杂度都为o(n),但是,如果数组是已经在程序给出的,我的方法不能在原数组上使用,而作者的方法可以直接在原数组上计算,
//所以我的空间复杂度是O(n),作者的空间复杂度是O(1)
public class Test3 {
public static void main(String[] args) {
Test3.authorMethod();
}
public static void myMethod() {
Scanner input = new Scanner(System.in);
int array_length = 0;
try {
array_length = input.nextInt();
}catch(InputMismatchException e) {
System.out.println("必须输入整数!");
}
int[] test = new int[array_length];
for(int count = 0;count< array_length;count++) {
try {
test[input.nextInt()] += 1;
}catch(InputMismatchException e) {
System.out.println("必须输入整数!");
}
}
for(int count = 0;count< array_length;count++) {
if(test[count] > 1) {
System.out.println(test[count]+"重复");
break;
}
}
}
public static void authorMethod() {
Scanner input = new Scanner(System.in);
int array_length = 7;
//
try {
array_length = input.nextInt();
if(array_length<=0) {
System.out.println("数组长度必须大于0");
return;
}
}catch(InputMismatchException e) {
System.out.println("必须输入整数!");
}
// int[] test = {2,3,1,0,2,5,3};
int[] test = new int[7];
// 数组不能 先new一个对象,在让他={2,3,1,0,2,5,3}
// test = {2,3,1,0,2,5,3};
for(int count = 0;count< array_length;count++) {
try {
test[count] = input.nextInt();
if(test[count]>=array_length||test[count]<0) {
System.out.println("必须输入0到数组长度(不包括数组长度本身)之间的数");
return;
}
}catch(InputMismatchException e) {
System.out.println("必须输入整数!");
}
}
for(int count = 0;count< array_length;count++) {
if(test[count] != count) {
int val = test[test[count]];
if(val == test[count]) {
System.out.println(test[count]+"重复");
// break;
}else {
test[test[count]] = test[count];
test[count] = val;
count--;
}
}
}
System.out.println("无重复数字");
}
}
题目二:不修改数组,找出数组中重复的数字。
具体描述:省略
一开始我的想法是从前往后遍历查找,但是这样时间复杂度是O(n^2)的方法并不会是好方法,最后看了书上的二分法的思路,代码如下。
//作者的的方法比我的好,因为没有用递归
public static void authorMethod2() {
Scanner input = new Scanner(System.in);
int array_length = 8;
//
// try {
// array_length = input.nextInt();
// if(array_length<=0) {
// System.out.println("数组长度必须大于0");
// return;
// }
// }catch(InputMismatchException e) {
// System.out.println("必须输入整数!");
// }
//
int[] test = {2,3,5,4,3,2,6,7};
// int[] test = new int[7];
// 数组不能 先new一个对象,在让他={2,3,1,0,2,5,3}
// test = {2,3,1,0,2,5,3};
// for(int count = 0;count< array_length;count++) {
// try {
//
// test[count] = input.nextInt();
// if(test[count]>=array_length||test[count]<0) {
// System.out.println("必须输入0到数组长度(不包括数组长度本身)之间的数");
// return;
//
// }
// }catch(InputMismatchException e) {
// System.out.println("必须输入整数!");
// }
// }
int start = 0;
int end = array_length-1;
int mid = 0;
while(end>start) {
mid = (end - start)/2 + start;
if(calculate(test,array_length,start,mid)>mid-start+1) {
end = mid;
}
if(calculate(test,array_length,mid+1,end)>end-mid) {
start = mid + 1;
}
}
System.out.println(""+test[mid]);
}
public static void myMethod2() {
Scanner input = new Scanner(System.in);
int array_length = 8;
//
// try {
// array_length = input.nextInt();
// if(array_length<=0) {
// System.out.println("数组长度必须大于0");
// return;
// }
// }catch(InputMismatchException e) {
// System.out.println("必须输入整数!");
// }
//
int[] test = {2,3,5,4,3,2,6,7};
// int[] test = new int[7];
// 数组不能 先new一个对象,在让他={2,3,1,0,2,5,3}
// test = {2,3,1,0,2,5,3};
// for(int count = 0;count< array_length;count++) {
// try {
//
// test[count] = input.nextInt();
// if(test[count]>=array_length||test[count]<0) {
// System.out.println("必须输入0到数组长度(不包括数组长度本身)之间的数");
// return;
//
// }
// }catch(InputMismatchException e) {
// System.out.println("必须输入整数!");
// }
// }
int result = Test3.quickfind(test, array_length, 0, array_length-1, array_length);
System.out.println(""+result);
}
public static int quickfind(int[] test, int array_length,int low,int high,int total_length) {
int result=0;
if(array_length>=2) {
int mark = low+array_length/2+array_length%2-1;
if(calculate(test,total_length,low,mark)>mark-low+1) {
result=quickfind(test, mark-low+1,low,mark,total_length);
}
if(calculate(test,total_length,mark+1,high)>high-mark) {
result=quickfind(test, high-mark,mark+1,high,total_length);
}
}else {
result = test[low];
return result;
}
return result;
}
public static int calculate(int[] test,int total_length,int low,int high) {
int time = 0;
for(int count = low; count <= high; count++) {
for(int count_inner = 0;count_inner < total_length;count_inner++) {
if(test[count] == test[count_inner]) {
time++;
}
}
}
return time;
}