Android查询算法

一、获取最小值

public int str(int str[]) {
    int min = str[0];
    for (int i = 1; i < str.length; i++) {
        if (min > str[i]) {
            min = str[i];
        }
    }
    return min;
}

二、递归数兔子

public static int fib(int i){
    if (i == 1){
        return 1;
    } else if (i == 2){
        return 1;
    } else {
        return fib(i - 1) + fib(i - 2);
    }
}

三、查找文件

public ArrayList<String> refreshFileList(String strPath) {
    String fileName;
    String suf = null;
    File dir = new File(strPath);
    File[] files = dir.listFiles();
    if (files == null) {
        return null;
    }
    List<String> list = new ArrayList<>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            refreshFileList(files[i].getAbsolutePath());
        } else {
            fileName= files[i].getName();
            int j = fileName.lastIndexOf(".");
            suf = fileName.substring(j + 1);
        }
        if (suf.equalsIgnoreCase("amr")) {
            list.add(files[i].getAbsolutePath());
        }
    }
    return list;
}

四、二分查找
(1)使用递归

public static int findIndext(int[] arr, int left, int right, int abc){
    if (arr == null || arr.length == 0){
        return -1;
    }
    if (left == right){
        if (arr[left] != abc){
            return -1;
        }
        return left;
    }
    int mid = left + (right - left) / 2;
    if (arr[mid] < abc){
        right = mid - 1;
        return findIndext(arr,left,right,abc);
    } else if (arr[mid] > abc){
        left = mid + 1;
        return findIndext(arr,left,right,abc);
    } else {
        return mid;
    }
}

(2)二分查找

public static int findIndext(int[] arr, int left, int right, int abc) {
    if (arr == null || arr.length == 0) {
        return -1;
    }
    if (left == right) {
        if (arr[left] != abc) {
            return -1;
        }
    }
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == abc) {
            return mid;
        } else if (arr[mid] < abc) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

五、字符串旋转

public static String reverseString(String x){
    if(x == null || x.length() < 2)
        return x;
    return reverseString(x.substring(1, x.length())) + x.charAT(0);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容