一些算法模板

递归的二分查找:

    int BinarySearchRecursion(int array[], int findData, int start, int end) {
        if (array == null || end <= 0)
            return -1;
        if (start > end)
            return -1;
        int mid = start + (end - start) / 2;
        if (array[mid] == findData)
            return mid;
        else if (findData < array[mid])
            return BinarySearchRecursion(array, findData, start, mid - 1);
        else
            return BinarySearchRecursion(array, findData, mid + 1, end);
    }

把终止条件写在最前面总是更清晰,不然就要这么写:

int BinSearch(int Array[],int low,int high,int key/*要找的值*/)
{
    if (low<=high)
    {
        int mid = (low+high)/2;
        if(key == Array[mid])
            return mid;
        else if(key<Array[mid])
            return BinSearch(Array,low,mid-1,key);
        else if(key>Array[mid])
            return BinSearch(Array,mid+1,high,key);
    }
    else
        return -1;
}

以下摘自:http://www.cnblogs.com/yueyebigdata/p/5126333.html
很乱,不知道怎么排版的。先简单放着。

3. UnionFind并查集模板

import java.util.HashMap;
import java.util.HashSet;


class UnionFind {
    HashMap<Integer, Integer> father = new HashMap();
    // 初始化
    UnionFind(HashSet<Integer> hashSet) {
        for (Integer now : hashSet) {
            father.put(now, now);
        }
    }
    // O(n)复杂度的find
//    public int find(int x) {
//        int parent = father.get(x);
//        while (parent != father.get(parent)) {
//            parent = father.get(parent);
//        }
//        return parent;
//    }
    // O(1)复杂度的find
    public int compressedFind(int x) {
        int parent = father.get(x);
        while (parent != father.get(parent)) {
            parent = father.get(parent);
        }
        // 这里parent是他的最大祖先。那么,包括他以及中间的各种都直接设置为指道最大祖先。
        int temp = -1;
        int xFa = x;
        while (xFa != father.get(xFa)) {
            temp = father.get(xFa); // 因为要把xFa的祖先设置为最大祖先了,所以,得先把踏上一级father记录下来。
            father.put(xFa, parent);
            xFa = temp;
        }
        return parent;
    }
    // x,y有一条边。所以,如果他们的祖先不一样,那么就把他们随便谁和谁连起来
    public void union(int x, int y) {
        int xFa = compressedFind(x);
        int yFa = compressedFind(y);
        if (xFa != yFa) {
            father.put(xFa, yFa);
        }
    }
}

2. 二分

非递归的,while-loop

public static int binarySearch(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return -1;
    }

递归的,recursion

public class Solution {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        System.out.println(binarySearch(a, 0, a.length-1, 6));
    }
    public static int binarySearch(int[] nums, int start, int end, int target) {
        if (nums == null || nums.length == 0 || start > end) {
            return -1;
        }
        int mid = start + (end - start) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] > target) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
        return binarySearch(nums, start, end, target);
    }
}

1. 非递归的二叉树遍历:

前序遍历:一句话概括就是:先放进去root,然后放进去顶元素的right,然后放进去left。

    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (null == root) {
            return result;
        }
        stack.push(root);
        while (!stack.empty()) {
            TreeNode cur = stack.pop();
            result.add(cur.val);
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
      

中序遍历:一句话总结概括:在进入while循环之前什么都不错,先把cur=root。进去之后,首先再用一个wihle一直往左走知道尽头,也一路的存入stack,然后把顶元素存入result,并且这时候把cur变成顶元素的右儿子。

    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (null == root) {
            return result;
        }
        TreeNode cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            result.add(cur.val);
            cur = cur.right;
        }
        return result;
    }

后序遍历:一句话总结,首先用一个cur,和一个pre。进入之前把root存进去。然后,只要是上一个处理的是null,或者是pre.left,pre.right等于现在的cur,就先把left存进去,如果left空,存进去right。然后,如果上一次处理的是当前结点的left,那么说明该处理右边了,所以把right存进去,如果以上两个情况都不是,那么就要存结果了,并且pop。记住就是最后的时候pre = cur。

    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if (null == root) {
            return result;
        }
        stack.push(root);
        TreeNode pre = null;
        TreeNode cur = root;
        while (!stack.empty()) {
            cur = stack.peek();
            if (pre == null || pre.left == cur || pre.right == cur) {
                if (cur.left != null) {
                    stack.push(cur.left);
                } else if (cur.right != null) {
                    stack.push(cur.right);
                }
            } else if (cur.left == pre) {
                if (cur.right != null) {
                    stack.push(cur.right);
                }
            } else {
                result.add(cur.val);
                stack.pop();
            }
            pre = cur;
        }
        return result;
    }

0. 下面一大堆大杂烩。

6移位操作

“>> 右移,高位补符号位” 这里右移一位表示除2
“>>> 无符号右移,高位补0”; 与>>类似
“<< 左移” 左移一位表示乘2,二位就表示4,就是2的n次方

6树的各种操作

import java.util.ArrayDeque;
import java.util.Queue;

public class Solution{

public static void main(String[] args){
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = null;
root.left.right =null;
root.right.left = null;
root.right.right =null;
levelOrder(root);
}
//树的遍历
//先序遍历
public static void preOrder(TreeNode root){
if(root == null) return;
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
//层次遍历
public static void levelOrder(TreeNode root){

Queue<TreeNode> q = new ArrayDeque<TreeNode>();
q.add(root);
while( !q.isEmpty()){
TreeNode tmp = q.poll();
System.out.println(tmp.val);

if(tmp.left != null ) q.add(tmp.left);
if(tmp.right != null ) q.add(tmp.right);
}

}

//树的高度
public static int treeHeight(TreeNode root){
int high = 0;
if(root == null) return 0;
high = 1 + Math.max(treeHeight(root.left), treeHeight(root.right));

return high;
}
//二叉排序树
public static boolean isBST(TreeNode root, int left, int right){
if(root == null) return true;
return (left < root.val && root.val < right && isBST(root.left,left ,root.val) && isBST(root.right,root.val,right));

}
public static boolean isBST(TreeNode root){
return isBST(root,Integer.MIN_VALUE, Integer.MAX_VALUE);
}



}
class TreeNode{
TreeNode left;
TreeNode right;
int val;
TreeNode(int x){
val = x;
}
}

5快速排序

public static void qSort(int[] a, int low, int high){
if(low >= high) return;
int first = low, last = high, key = a[first];
while(first < last){
while(first < last && a[last] >= key) --last;
a[first] = a[last];
while(first < last && a[first] <= key) ++first;
a[last] = a[first];
}
a[first] = key;
qSort(a, low, first - 1);
qSort(a, last + 1, high);
}

4归并排序(背下来)。

public static void mergeSort(int[] a, int[] temp, int left, int right){
if(left >= right) return;
int mid = (left + right) / 2;

mergeSort(a, temp, left, mid);
mergeSort(a, temp, mid+1, right);

int p_final = left, p_left = left, p_right = mid + 1;
while(p_left <= mid || p_right <= right){

if(p_left <= mid && (p_right >right || a[p_left] <= a[p_right])){
temp[p_final++] = a[p_left++];
}
else{
temp[p_final++] = a[p_right++];
}
}
for ( int i = left; i <= right; i++){
a[i] = temp[i];
}

}

3. 二分函数(背下来)。

public static int binSearch(int[] a, int key){
int left = 0;
int right = a.length - 1;
while(left <= right){
int mid = (left + right) / 2;
if(key < a[mid]){
right = mid -1;
}
else if (key > a[mid]){
left = mid + 1;
}
else{
return mid;
}
}
return -1;
}

1. java的swap函数。交换

public  static  void  swap ( int [] data,  int  a,  int  b) {

int  t = data [a];      

        data [a] = data [b];      

        data [b] = t;      

}  

2. 数组翻转函数(reverse)

2.向右移动数组k位(rotate array);

public class Solution {
public void rotate(int[] nums, int k) {
if(nums.length <= 1){
return;
}
//step each time to move
int step = k % nums.length;
reverse(nums,0,nums.length - 1);//放到最后就是向左移
reverse(nums,step,nums.length - 1);
reverse(nums,0,step - 1);
}

//reverse int array from n to m
public void reverse(int[] nums, int n, int m){
while(n < m){
nums[n] ^= nums[m];
nums[m] ^= nums[n];
nums[n] ^= nums[m];
n++;
m--;
}
}
}

3. 字符串转成字符数组

s.toCharArray();

方法1:直接在构造String时转换。
char[] data = {'a', 'b', 'c'};
String str = new String(data);

方法2:调用String类的方法转换。
String.valueOf(char[] ch)

4.字符串翻转(String reverse)

new String

整数数组转化成字符串

Arrays.toString(a)


补充:我曾写过的快速排序和冒泡排序
quicksort
43125第一次循环后是23145, 4是pivot,4左边的都比它小,右边的都比它大。注意一定是j先往左走,不然结果不对。

public class Qksort{
    public static void sort(int array[] , int low , int high ){
        int i = low , j = high , index = array[i];
        if(low >= high )return ;
        while (i<j)
        {
            while(i<j && array[j]>= index)
                j-- ;
            if(i< j )
                array[i++] = array[j];
            while(i<j && array[i]<= index)
                i++ ;
            if(i<j )
                array[j--] = array[i];
        }
        array[i]= index ;
        sort(array, low , i - 1);
        sort(array, i + 1 , high );
    }
    public static void main(String args[]){
        int a[] = {4,3,1,2,5};
        sort(a, 0 , a.length - 1);
        for(int i = 0 ; i < a.length ; i++)
            System.out.println(a[i]);
    }

}

bubblesort

public class BBsort{
    private static void BBsort(int array[])
    {
        int temp ;
        int len = array.length;
        for(int i = 0 ; i < len-1 ; i++)
        {
            for(int j = 0 ; j < len - i - 1 ; j ++ )
            {
                if(array[j]>array[j+1])
                {
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp ;
                    
                }
            
            }
        }
    
    
    }
    
    public static void main(String args[])
    {
        int a[] = {4,3,1,5,6,1,2,4,8};
        BBsort(a);
        for(int i = 1 ; i < a.length ; i ++ )
            System.out.println(a[i]);
    
    }

}

另:
https://github.com/Yuelinghui/personNote/blob/master/10大基础实用算法.md

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容