java常用方法总结

最近打算换工作,还是需要补一下面试的基础知识,写了一些面试中可能会用到的常用算法、方法,以便复习

    //99乘法表
    /**
     * 1*1
     * 1*1 1*2
     * 1*1 1*2 1*3
     * ……
     * */
    private static void chengfabiao() {
        for(int i=1;i<=9;i++) {
            for(int j=1;j<=i;j++) {
                System.out.print(j+"*"+i+"="+i*j+";");
            }
            System.out.println();
        }
    }
}

斐波那契数列:

//递归
    private static int feibonaqie(int index) {
        // TODO Auto-generated method stub
        //0 1 1 2 3 5 8 13 21
        int temp = 0;
        
        if(index == 0) {
            return 0;
        }else if(index == 1) {
            return 1;
        }else {
            temp = feibonaqie(index-2)+feibonaqie(index-1);
            //sum += temp;
            //count++;
            return temp;
        }
        
    }
//循环    
    private static int feibonaqie2(int index) {
        // TODO Auto-generated method stub
        //0 1 1 2 3 5 8 13 21
        int temp = 0;
        int a = 0,b = 1;
        if(index == 0) {
            return 0;
        }else if(index == 1) {
            return 1;
        }else {
            
            for(int i=2;i<=index;i++) {
                temp = a+b;
                a=b;
                b=temp;
            }
            //sum += temp;
            //count++;
            return temp;
        }
        
    }

 

/** 
 * @author QiaoJiafei 
 * @version 创建时间:2016年3月9日 下午12:02:09 
 * 类说明 获取数组平衡点
 */
public class TestArrayBlance {
    public static void main(String[] args) {
        int arr[] = {10,5,4,1,3,7};
        getBlance(arr);
    }

    private static void getBlance(int arry[]) {
        // TODO Auto-generated method stub
        int sum = 0;
        int subsum = 0;
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<arry.length;i++) {
            sum += arry[i];
        }
        for(int i=0;i<arry.length;i++) {
            subsum += arry[i];
            if(subsum == sum-subsum){
                list.add(i);
            }
            for(int j=i+1;j<arry.length;j++) {
                
                if(subsum == sum-subsum-arry[j]) {
                    list.add(j);
                }
            }
        }
        System.out.println(list.size()+";"+list.get(0));
    }
}

 

/*
 * 4,模拟一个trim功能一致的方法。去除字符串两端的空白 
 * 思路:
 * 1,定义两个变量。
 * 一个变量作为从头开始判断字符串空格的角标。不断++。
 * 一个变量作为从尾开始判断字符串空格的角标。不断--。
 * 2,判断到不是空格为止,取头尾之间的字符串即可。
 */
public class StringTest_4 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String s = "    ab   c     ";

        s = myTrim(s);
        System.out.println("-" + s + "-");
    }

    public static String myTrim(String s) {

        int start = 0, end = s.length() - 1;

        while (start <= end && s.charAt(start) == ' ') {
            start++;
        }
        while (start <= end && s.charAt(end) == ' ') {
            end--;
        }
        return s.substring(start, end + 1);
    }

}

 

/** 
 * @author QiaoJiafei 
 * @version 创建时间:2016年1月25日 上午11:52:50 
 * 类说明 获取最大子串。思路每次循环从子串的第一个位置开始start,以子串总长度-i结束end。整体依次往右移,这样循环。i代表循环次数,从0开始
 */
public class StringContents {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String s1 = "asdflkjljsodfjiosdjf";
        String s2 = "jsodfjia";
        fff(s1, s2);
    }

    public static void fff(String s1, String s2) {
        boolean flag = false;
        int len = s2.length();
        for (int  i= 0;  i< len; i++) {
            for (int a = 0,b=len-i; b<=len; a++,b++) {
                String s = s2.substring(a, b);
                //System.out.println(s);
                if(s1.contains(s)) {
                    System.out.println("index:"+s1.indexOf(s));
                    System.out.println(s);
                    flag = true;
                    return;
                }
            }
            if(flag) {
                break;
            }
        }
    }
}

 

 

 

public class Test {  
public static void main(String args[]) { int [] arry = {5,45,2,1,8,6,12}; Test t = new Test(); } //冒泡排序 void maoP(int a[]) { int temp = 0; for (int i=0;i<a.length-1;i++) { for(int j=0; j<a.length-1-i; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } void prin(int a[]) { for (int i=0; i<a.length; i++) { System.out.print(a[i] + " "); } } //选择排序 void xuanZ(int a[]) { for(int i=0; i<a.length; i++) { //int p = a[i]; int temp = 0; for (int j=i+1; j< a.length; j++) { if (a[j] < a[i]) { temp = a[i]; a[i] = a[j]; a[j] =temp; //a[j] = temp; } } } } //快速排序 void kuaiS(int []a, int low, int high) { if (low < high) { int mid = kP(a, low, high); kuaiS(a, low, mid-1); kuaiS(a, mid+1, high); } } int kP(int []a, int low, int high) { int temp = a[low]; while (low < high) { while (low < high && temp <= a[high]) { high--; } a[low] = a[high]; while (low < high && temp >= a[low]) { low++; } a[high] = a[low]; } a[low] = temp; return low; } //二分查找-普通循环 int erF(int a[], int tp) { int mid = a.length/2; int start = 0; int end = a.length-1; while (start <= end) { mid = (end - start)/2 + start; if (tp == a[mid]) { return mid; } else if (tp < a[mid]) { end = mid -1; } else if (tp > a[mid]) { start = mid + 1; } else { return mid; } } return -1; } //二分查找-递归 int erFeD(int a[],int dex, int start, int end) { int mid = (end - start)/2 + start; if (a[mid] == dex) { return mid; } if (end <= start) { return -1; } else if (a[mid] > dex) { return erFeD(a, dex, start, mid-1); } else if (a[mid] < dex) { return erFeD(a, dex, mid+1, end); } return -1; } //顺序查找 int shunX(int a[], int dex) { for (int i=0; i< a.length-1; i++) { if(a[i] == dex) { return i; } } return -1; } //阶乘 void jieC(int temp) { int f = 1; for (int i=1; i<=temp; i++) { f = f*i; } System.out.println(f); } //阶乘相加 void JieChJ(int temp) { int sum = 0; int f = 1; for (int i=1; i<=temp; i++) { f *= i; sum += f; } System.out.println(sum); } //阶乘相加2 int jieC1(int temp) { int f = 1; for (int i=1; i<=temp; i++) { f = f*i; } return f; } void jieC2(int temp) { int sum = 0; for (int i=1; i<=temp; i++) { sum += jieC1(i); } System.out.println(sum); } //1~100的质数 void zhiS() { for (int i=1; i<100; i+=2) { boolean flag = true; for (int j=2; j<i; j++) { if (i%j == 0) { flag = false; break; } } if (!flag) { continue; } System.out.print(i + " "); } } //1~100前5个被3整除的数 void zhengC() { int temp = 0; for (int i=1; i<=100; i++) { if (i%3 == 0) { temp++; System.out.print(i + " "); } if (temp == 5) { break; } } } }

 

输出:

0

12

345

6789

    static void fString() {
        String s = "0123456789";
        int sum = 0;
        for(int i=0;i<4;i++) {
            sum +=i;
            System.out.println(s.substring(sum, sum+i+1));
        }
    }

一个数组中的元素出现了3次的值

    private static int getArrayValue(int array[]) {
        // TODO Auto-generated method stub
        int value = 0;
        for(int i=0;i<array.length;i++) {
            int count = 0;
            for(int j=0;j<array.length;j++) {
                if (array[i] == array[j]) {
                    count ++;
                }
            }
            if(count == 3) {
                value = array[i];
            }
        }
        return value;
    }

 大小写转换

    public static void main(String[] args) {
        String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        int num = 123433568;
        String s = String.valueOf(num);
        char c[] =s.toCharArray();
        
        for(int i=0;i<c.length;i++) {
            int j = Integer.parseInt(String.valueOf(c[i]));
            System.out.print(u[j]);
        }
    }

 

posted on 2016-03-15 10:14  乔叶叶  阅读(527)  评论(0编辑  收藏  举报

导航