import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class atest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int[] a = {1,3,7,9};
// int[] b = {3,4,11,22,33};
// int[] c = ab(a,b);
// int[] b = {12,4,11,22,33};
// int[] c = sort(b);
// for (int i =0;i<c.length;i++) {
// System.out.print(c[i]+" ");
// }
//
// int[] b = {12,12,11,22,33,33,33};
// List c = findmullist(b);
// System.out.println(c);
// readfile();
// readfilebuf();
// filewrite();
// filebufwrite();
// char c = 74;
// System.out.println(c);
// String s1 = new String("aa");
// String s2 = new String("aa");
// boolean b0 = s1==s2; //false
// boolean b1 = "12"=="12"; //true
// boolean b2 = "12".equals("12");
// System.out.println(b0+","+b1+","+b2);
// testcases();
// findlongest("aseswertttttqwer");
// findways(5);
// int[] array = {3,2,8};
// int result = findsummid(array);
// String result = reverse("Hello Word!");
// boolean result = plalindrome("qwawq");
// int[][] array = {{1,2,3},{4,5,6}};
// int[] result = clockwise(array);
// System.out.println(result);
int[] array = {1,8,3,2,7,12,5};
getindex(array,10);
}
/**
* 找出数组中两个数的和等于目标数字
* @param array
* @param obj
* @return
*/
public static int[] getindex(int[] array, int obj) {
int[] result = new int[array.length];
int index = 0;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0; i<array.length; i++) {
map.put(obj-array[i], i);
}
for(int i=0; i<array.length;i++ ) {
if(map.containsKey(array[i])&& i!=map.get(array[i])) {
result[index++]=i;
result[index++]=map.get(array[i]);
map.remove(array[i]);
map.remove(obj-array[i]);
}
}
for(int i=0;i<result.length;i++) {
System.out.print(result[i]);
}
return result;
}
/**顺时针输出二维数组{{1,2,3},{4,5,6}};123654
* @param array
* @return
*/
public static int[] clockwise(int[][] array) {
int [] result = new int[array.length*array[0].length];
for(int i=0; i<(array.length); i++) {
for(int j=0; j< array[0].length; j++) {
if(i%2 == 0) {
result[i*array[0].length+j]=array[i][j];
}else {
result[i*array[0].length+j]=array[i][array[0].length-j-1];
}
}
}
for(int i=0;i<result.length;i++) {
System.out.print(result[i]);
}
return result;
}
/**判断字符串是否是回文
* @param str
* @return
*/
public static boolean plalindrome(String str) {
boolean result = true;
char[] chars = str.toCharArray();
for(int i=0; i<(str.length()/2);i++) {
if(!(chars[i]==chars[str.length()-1-i])) {
result = false;
break;
}
}
return result;
}
/**
* 用递归方式将字符串倒置
* @param str
* @return
*/
public static String reverse(String str) {
int length = str.length();
if(length==1) {
return str;
}else {
return str.substring(length-1,length)+reverse(str.substring(0, length-1));
}
}
/**找到最大的数n,前n个数的和小于后面数的和
* @param arrayint
* @return i数组下标
*/
public static int findsummid(int[] arrayint) {
int result = 0;
int i=0;
int j=arrayint.length-1;
int startsum = 0;
int endsum = 0 ;
for(;i<=j;) {
if(startsum < endsum) {
startsum += arrayint[i++];
}else {
endsum += arrayint[j--];
}
}
if(startsum > endsum){ i -= 2; }else{ i--; }
return i;
}
/**
* N级台阶,每次只能有1个台阶或者2个台阶,N个台阶有几种可能
* @param floor
* @return
*/
public static int findways(int floor) {
int result =0;
if(floor<1) {
return result;
}
if(floor == 1) {
result =1;
}
if(floor == 2) {
result = 2;
}
if(floor>2) {
result = findways(floor-1)+findways(floor-2);
}
System.out.println(floor+":"+result);
return result;
}
/**
* 查找字符串中最无不重复字符串,并给出长度aseswertttttqwer,{tqwer=5, swert=5}
* @param str
* @return
*/
public static Map findlongest(String str){
Map<String,Integer> map = new HashMap<String,Integer>();
char[] arraychar = str.toCharArray();
List<Character> temp = new LinkedList<Character>();
int max = 0;
int start = 0;
for(int i=0; i<arraychar.length;i++) {
Set set = new HashSet(temp);
boolean flag = set.add(arraychar[i]);
if(!flag) {
temp.clear();
start = i;
}
temp.add(arraychar[i]);
if(temp.size()>=max) {
if(temp.size()>max) {
map.clear();
max = temp.size();
}
map.put(str.substring(start, i+1), temp.size());
}
}
System.out.println(map.toString());
return map;
}
public static void testcases() {
int i=9;
switch (i) {
default:
System.out.println("default");
case 0:
System.out.println("zero");
// break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
}
}
//返回2
public static int get()
{
try {
return 1;
}finally{
return 2;
}
}
public static void filebufwrite() {
try {
FileWriter fwriter = new FileWriter("D:\\eclipse-workspace\\demo\\autotest\\myfile.txt");
BufferedWriter out = new BufferedWriter(fwriter);
out.write("hello Word2!\n");
out.newLine();
out.write("\n");
out.write("世界你好!\n");
out.close();
fwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void filewrite() {
try {
FileWriter fwriter = new FileWriter("D:\\eclipse-workspace\\demo\\autotest\\myfile.txt");
//文件末尾追加
// FileWriter fwriter = new FileWriter("D:\\eclipse-workspace\\demo\\autotest\\myfile.txt",true);
fwriter.write("hello Word!\n");
fwriter.write("\n");
fwriter.write("你好,世界!\n");
fwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readfile() {
try {
char[] buf = new char[1024];
int num =0;
FileReader reader = new FileReader("D:\\eclipse-workspace\\demo\\autotest\\readme.txt");
try {
while((num = reader.read(buf))!= -1) {
System.out.print(new String(buf,0,num));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readfilebuf() {
try {
FileReader filereader = new FileReader("D:\\eclipse-workspace\\demo\\autotest\\readme.txt");
BufferedReader bufreader = new BufferedReader(filereader);
String line = null;
while((line = bufreader.readLine())!= null) {
System.out.println(line);
}
bufreader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**找出一个随机数组中出现次数最多的数字,多个一样多的次数,都返回
* @param a
* @return
*/
public static List findmullist(int[] a) {
List<Integer> mul = new LinkedList<Integer>();
int maxtime = 1;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
if(a.length>0){
mul.add(a[0]);
}else {
return null;
}
for(int i=0; i<a.length; i++) {
int times = 0;
if(map.get(a[i])!=null) {
times = map.get(a[i]);
}
map.put(a[i], ++times);
if(times>maxtime) {
maxtime = times;
mul.clear();
mul.add(a[i]);
}else if(times == maxtime){
mul.add(a[i]);
}
}
return mul;
}
/**找出一个随机数组中出现次数最多的数字,多个一样多的返回第一个
* @param a
* @return
*/
public static int findmul(int[] a) {
int mul = 0 ;
int maxtime = 1;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
if(a.length>0){
mul = a[0];
}else {
return -1;
}
for(int i=0; i<a.length; i++) {
int times = 0;
if(map.get(a[i])!=null) {
times = map.get(a[i]);
}
map.put(a[i], ++times);
if(times>maxtime) {
maxtime = times;
mul = a[i];
}
}
return mul;
}
/**使用冒泡排序对数组进行排列
* @param a
* @return
*/
public static int[] sort(int[] a) {
boolean flag = true;
for(int i=0; i<a.length && flag ; i++) {
flag = false;
for(int j=0; j<a.length-i-1; j++) {
if(a[j]>a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = true;
}
}
System.out.println(i);
}
return a;
}
/**合并两个有序数组为一个数组
* @param a
* @param b
* @return
*/
public static int[] ab(int[] a, int[] b) {
int[] c = new int[a.length+b.length];
int indexa=0;
int indexb=0;
for (int i =0;i<c.length;i++) {
if(indexa<a.length && indexb<b.length) {
if(a[indexa]>b[indexb]) {
c[i] = b[indexb++];
}else {
c[i] = a[indexa++];
}
}else {
if(indexa>=a.length) {
c[i] = b[indexb++];
}else {
c[i] = a[indexa++];
}
}
}
return c;
}
}