Android开发Android开发经验谈Android技术知识

Android面试题——算法篇

2019-04-26  本文已影响8人  迷途小码农h

前言

正文

实现阶乘

//采用递归法

    if (number <= 1)
        return 1;
    else
        return number * factorial(number - 1);
}
//采用循环连乘法    
public static int fact(int num){
    int temp=1;
    int factorial=1;
    while(num>=temp){
    factorial=factorial*temp;
    temp++;
    }
    return factorial;
}
二分查找

//递归法

    if (low > high) return -1;
    int mid = low + (high - low) / 2;
    if (array[mid] > target)
        return binarysearch(array, low, mid - 1, target);
    if (array[mid] < target)
        return binarysearch(array, mid + 1, high, target);
    return mid;
}

//循环法

     int low = 0;
    int high = a.length - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (a[mid] > key)
            high = mid - 1;
        else if (a[mid] < key)
            low = mid + 1;
        else
                return mid;
    }
     return -1;
}
二分查找中值的计算
二分查找法的缺陷
用两个栈实现队列

题目描述:

    
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node){
        stack1.push(node);
    }
    
    public int pop(){
        if(stack2.empty()){
            while(!stack1.empty())
                stack2.push(stack1.pop());
        }
        return stack2.pop();
}
递归和迭代的区别是什么,各有什么优缺点?
    public static void main(String args[]){
        int i=0;
        for(i=101;i<=200;i++)
        if(math.isPrime(i)==true)
            System.out.println(i);
    }
}
class math
{
    //方法1
    public static boolean isPrime(int x)
    {
        for (int i=2;i<=x/2;i++)
            if (x%2==0)
                return false;
        return true;
    }
    
    //方法2
    public static boolean isPrime2(int m)
    {
        int k=(int)sqrt((double)m);
        for (int i=2;i<=k;i++)
            if (m%i==0)
                    return false;
        return true;
    }
}

字符串小写字母转换成大写字母
public String toUpperCase(String str)
{
    if (str != null && str.length() > 0) {
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
         c += 32;
      }
   }
    return str;
}
进制转换:给定一个十进制数 n 和 一个整数 k, 将 十进制数 n 转换成 k进制数
    StringBuffer resultNumber = new StringBuffer();
    tenToK(resultNumber, n, k);
    System.out.println("n:k:result: " + n +" "+ k + " " + resultNumber.toString());

    return resultNumber.toString();
}

private void tenToK(StringBuffer stringBuffer, int n, int k) {
    int integral = n/k;
    int mode = n % k;
   stringBuffer.insert(0, mode);
    if (integral >= k) {
        tenToK(stringBuffer, integral, k);
   } else if (integral > 0) {
        stringBuffer.insert(0, integral);
   }
}

位运算实现加法
public int aplusb(int a, int b) {
    int sum_without_carry, carry;

    sum_without_carry = a^b; //没有进位的和
    carry = (a&b)<<1; //进位
    if(carry==0) {
        return sum_without_carry;
    } else {
        return aplusb(sum_without_carry, carry);
    }
}

二叉树排序树
首先定义节点类

    Object obj;
    TreeNode parent;
    TreeNode lchild;
    TreeNode rchild;
      
    public TreeNode(int obj) {
        this.obj = obj;
    }
}
然后创建一个树类

public class Tree {
      
    /**
     * 先序遍历二叉树 
     * @param root
     */  
    public void Fprint(TreeNode root){
        if(root!=null){
            System.out.println(root.obj);
            Fprint(root.lchild);
            Fprint(root.rchild);
        }
    }
      
    /**
     * 中序遍历二叉树 
     * @param root
     */  
    public void Mprint(TreeNode root){
        if(root!=null){
            Mprint(root.lchild);
            System.out.println(root.obj);
              
            Mprint(root.rchild);
        }
    }
      
    /**
     * 根据一个int数组建立二叉排序树 
     * @param a 数组 
     * @return
     */  
    public TreeNode Build(int[] a){
        if(a.length==0){
            return null;
        }
        TreeNode root = new TreeNode(a[0]);
        for(int i=1;i<a.length;i++){
            TreeNode newnode = new TreeNode(a[i]);
            sort(root,newnode);
        }
        return root;
    }
    /**
     * 在二叉排序树中添加一个节点 
     * @param root 二叉树的根节点 
     * @param newnode 新加入的加点 
     * @return
     */  
    public void sort(TreeNode root,TreeNode newnode){
        TreeNode node = root;
        if((Integer)newnode.obj<=(Integer)node.obj){
            if(node.lchild==null){
                newnode.parent = node;
                node.lchild = newnode;
            }else{
                sort(node.lchild,newnode);
            }
        }else{
            if(node.rchild==null){
                newnode.parent = node;
                node.rchild = newnode;
            }else{
                sort(node.rchild,newnode);
            }
        }
    }
}

创建二叉排序树的时候随便传入一个int型数组a[]
然后通过自顶向下的方式一个一个的将a[0]---a[n]个元素创建的节点类一个一个的拼接到树上
此后只需要再创建一个主函数类来调用便行了

  
    public static void main(String[] args) {
        int a[] = {100,35,3,44,212,453};
        Tree t = new Tree();
        TreeNode root = t.Build(a);
        t.Mprint(root);
    }
  
}
冒泡排序
 
/**
 * 冒泡排序
 * 平均O(n^2),最好O(n),最坏O(n^2);空间复杂度O(1);稳定;简单
 * @author zeng
 *
 */
public class BubbleSort {
     
    public static void bubbleSort(int[] a){
 
        int n = a.length;
        int temp = 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n-i-1;j++){
                if(a[j]<a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }
     
    public static void main(String[] args){
        int[] a ={49,38,65,97,76,13,27,50};
        bubbleSort(a);
        for(int j:a)
            System.out.print(j+" ");
    }
}
插入排序
 
/**
 * 插入排序
 * 平均O(n^2),最好O(n),最坏O(n^2);空间复杂度O(1);稳定;简单
 * @author zeng
 *
 */
public class InsertionSort {
 
    public static void insertionSort(int[] a) {
        int tmp;
        for (int i = 1; i < a.length; i++) {
            for (int j = i; j > 0; j--) {
                if (a[j] < a[j - 1]) {
                    tmp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = tmp;
                }
            }
        }
    }
 
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 50 };
        insertionSort(a);
        for (int i : a)
            System.out.print(i + " ");
    }
}
选择排序
 
/**
 * 选择排序
 * 平均O(n^2),最好O(n^2),最坏O(n^2);空间复杂度O(1);不稳定;简单
 * @author zeng
 *
 */
public class SelectionSort {
 
    public static void selectionSort(int[] a) {
        int n = a.length;
        for (int i = 0; i < n; i++) {
            int k = i;
            // 找出最小值的小标
            for (int j = i + 1; j < n; j++) {
                if (a[j] < a[k]) {
                    k = j;
                }
            }
            // 将最小值放到排序序列末尾
            if (k > i) {
                int tmp = a[i];
                a[i] = a[k];
                a[k] = tmp;
            }
        }
    }
 
    public static void main(String[] args) {
        int[] b = { 49, 38, 65, 97, 76, 13, 27, 50 };
        selectionSort(b);
        for (int i : b)
            System.out.print(i + " ");
    }
}
快速排序
 
/**
 * 快速排序
 * 平均O(nlogn),最好O(nlogn),最坏O(n^2);空间复杂度O(nlogn);不稳定;较复杂
 * @author zeng
 *
 */
public class QuickSort {
 
    public static void sort(int[] a, int low, int high) {
        if(low>=high)
            return;
        int i = low;
        int j = high;
        int key = a[i];
        while (i < j) {
            while (i < j && a[j] >= key)
                j--;
            a[i++] = a[j];
            while (i < j && a[i] <= key)
                i++;
            a[j--] = a[i];
        }
        a[i] = key;
        sort(a,low,i-1);
        sort(a,i+1,high);
    }
 
    public static void quickSort(int[] a) {
        sort(a, 0, a.length-1);
        for(int i:a)
            System.out.print(i+" ");
    }
 
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 50 };
        quickSort(a);
    }
}
归并排序
 
/**
 * 归并排序
 * 平均O(nlogn),最好O(nlogn),最坏O(nlogn);空间复杂度O(n);稳定;较复杂
 * @author zeng
 *
 */
public class MergeSort {
 
    public static void merge(int[] a, int start, int mid,
            int end) {
        int[] tmp = new int[a.length];
        System.out.println("merge " + start + "~" + end);
        int i = start, j = mid + 1, k = start;
        while (i != mid + 1 && j != end + 1) {
            if (a[i] < a[j])
                tmp[k++] = a[i++];
            else
                tmp[k++] = a[j++];
        }
        while (i != mid + 1)
            tmp[k++] = a[i++];
        while (j != end + 1)
            tmp[k++] = a[j++];
        for (i = start; i <= end; i++)
            a[i] = tmp[i];
        for (int p : a)
            System.out.print(p + " ");
        System.out.println();
    }
 
    static void mergeSort(int[] a, int start, int end) {
        if (start < end) {
            int mid = (start + end) / 2;
            mergeSort(a, start, mid);// 左边有序
            mergeSort(a, mid + 1, end);// 右边有序
            merge(a, start, mid, end);
        }
    }
 
    public static void main(String[] args) {
        int[] b = { 49, 38, 65, 97, 76, 13, 27, 50 };
        mergeSort(b, 0, b.length - 1);
    }
}
希尔排序
 
/**
 * 希尔排序
 * 平均O(nlogn),最坏O(nlogn);空间复杂度O(1);不稳定;较复杂
 * @author zeng
 *
 */
public class ShellSort {
 
    public static void shellSort(int[] a) {
        int n = a.length;
        int d = n / 2;
        while (d > 0) {
            for (int i = d; i < n; i++) {
                int j = i - d;
                while (j >= 0 && a[j] > a[j + d]) {
                    int tmp = a[j];
                    a[j] = a[j + d];
                    a[j + d] = tmp;
                    j = j - d;
                }
            }
            d = d / 2;
        }
    }
 
    public static void main(String[] args) {
        int[] b = { 49, 38, 65, 97, 76, 13, 27, 50 };
        shellSort(b);
        for (int i : b)
            System.out.print(i + " ");
    }
}
基数排序
 
/**
 * 基数排序
 * 平均O(d(n+r)),最好O(d(n+r)),最坏O(d(n+r));空间复杂度O(n+r);稳定;较复杂
 * d为位数,r为分配后链表的个数
 * @author zeng
 *
 */
public class RadixSort {
 
    //pos=1表示个位,pos=2表示十位
    public static int getNumInPos(int num, int pos) {
        int tmp = 1;
        for (int i = 0; i < pos - 1; i++) {
            tmp *= 10;
        }
        return (num / tmp) % 10;
    }
 
    //求得最大位数d
    public static int getMaxWeishu(int[] a) {
        int max = a[0];
        for (int i = 0; i < a.length; i++) {
            if (a[i] > max)
                max = a[i];
        }
        int tmp = 1, d = 1;
        while (true) {
            tmp *= 10;
            if (max / tmp != 0) {
                d++;
            } else
                break;
        }
        return d;
    }
 
    public static void radixSort(int[] a, int d) {
 
        int[][] array = new int[10][a.length + 1];
        for (int i = 0; i < 10; i++) {
            array[i][0] = 0;// array[i][0]记录第i行数据的个数
        }
        for (int pos = 1; pos <= d; pos++) {
            for (int i = 0; i < a.length; i++) {// 分配过程
                int row = getNumInPos(a[i], pos);
                int col = ++array[row][0];
                array[row][col] = a[i];
            }
            for (int row = 0, i = 0; row < 10; row++) {// 收集过程
                for (int col = 1; col <= array[row][0]; col++) {
                    a[i++] = array[row][col];
                }
                array[row][0] = 0;// 复位,下一个pos时还需使用
            }
        }
    }
 
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 197, 76, 213, 27, 50 };
        radixSort(a, getMaxWeishu(a));
        for (int i : a)
            System.out.print(i + " ");
    }
}
堆排序
 
/**
 * 堆排序
 * 平均O(nlogn),最好O(nlogn),最坏O(nlogn);空间复杂度O(1);不稳定;较复杂
 * @author zeng
 *
 */
public class HeapSort {
 
    public static void heapSort(int[] a) {
        int i;
        int len = a.length;
        // 构建堆
        for (i = len / 2 - 1; i >= 0; i--)
            heapAdjust(a, i, len - 1);
        //交换堆顶元素与最后一个元素的位置
        for (i = len - 1; i > 0; i--) {
            int tmp = a[0];
            a[0] = a[i];
            a[i] = tmp;
            heapAdjust(a, 0, i - 1);
        }
    }
 
    public static void heapAdjust(int[] a, int pos, int len) {
        int child = 2 * pos + 1;
        int tmp = a[pos];
        while (child <= len) {
            if (child < len && a[child] < a[child + 1])
                child++;
            if (a[child] > tmp) {
                a[pos] = a[child];
                pos = child;
                child = 2 * pos + 1;
            } else
                break;
        }
        a[pos] = tmp;
    }
 
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 50 };
        heapSort(a);
        for (int i : a)
            System.out.print(i + " ");
    }
}

总结

以上就是这篇文章的内容了,喜欢的可以关注一下哦,也可以加android学习交流群1005956838,谢谢!

最后

希望大家能有一个好心态,想进什么样的公司要想清楚,并不一定是大公司,我选的也不是特大厂。当然如果你不知道选或是没有规划,那就选大公司!希望我们能先选好想去的公司再投或内推,而不是有一个公司要我我就去!还有就是不要害怕,也不要有压力,平常心对待就行,但准备要充足。最后希望大家都能拿到一份满意的 offer !如果目前有一份工作也请好好珍惜好好努力,找工作其实挺累挺辛苦的。

上一篇 下一篇

猜你喜欢

热点阅读