zoukankan      html  css  js  c++  java
  • 剑指offer题目--算法和数据结构

    2.4.1查找和排序

    二分查找、归并排序和快速排序

    递归和循环两种方式

    顺序查找、二分查找、哈希表查找、二叉排序树查找、

    1.查找

    在排序的数组(或者部分排序的数组)中查找一个数字或者统计某个数字出现的次数。可以用二分查找算法。

    哈希表和二叉排序树查找的重点在于考察对应的数据结构而不是算法。

    哈希表最主要的优点是我们能利用它在O(1)时间查找某一元素,是效率最高的查找方式,缺点是需要额外的空间来实现哈希表。

    35 第一个只出现一次的字符

    与二叉排序树查找算法对应的数据结构是二叉搜索树

    24 二叉搜索树的后续遍历序列

    2.排序

    插入排序、冒泡排序、归并排序、快速排序

    长度为n的数组中查找底k大的数字

    29 数组中出现次数超过一半的数字

    30 最小的k个数

    8 旋转数组的最小数字

    import java.util.ArrayList;
    public class Solution {
        public int minNumberInRotateArray(int [] array) {
            if(array == null || array.length <=0){
                return 0;
            }
            int index1 = 0;
            int index2 = array.length-1;
            int indexMid = index1;
            while(array[index1] >= array[index2]){
                if(index2 - index1 == 1){
                    indexMid = index2;
                    break;
                }
                
                indexMid = (index1+index2)/2;
                
                if(array[indexMid] >= array[index1]){
                    index1 = indexMid;
                }else if(array[indexMid] <= array[index2]){
                    index2 = indexMid;
                }
            }
            return array[indexMid];
        }
    }

    2.4.2 递归和循环

    9 斐波那契数列

    public class Solution {
        public int Fibonacci(int n) {
            int preNum=1;
            int prePreNum=0;
            int result=0;
            if(n==0)
                return 0;
            if(n==1)
                return 1;
            for(int i=2;i<=n;i++){
                result=preNum+prePreNum;
                prePreNum=preNum;
                preNum=result;
            }
            return result;
        }
    }

    青蛙跳台阶

    public class Solution {
        public int JumpFloor(int target) {
            if(target<2)
                return target;
            int f1=1;
            int f2=0;
            int f=0;
            for(int i=1;i<=target;++i)
                {
                f=f1+f2;
                f2=f1;
                f1=f;
            }
            return f;
        }
    }

    变态跳台阶

    public class Solution {
        public int JumpFloorII(int target) {
            if (target <= 0) {
                return -1;
            } else if (target == 1) {
                return 1;
            } else {
                return 2 * JumpFloorII(target - 1);
            }
        }
    }

    矩形覆盖

    public class Solution {
        public int RectCover(int target) {
            if (target <= 2){
                return target;
            }
            int pre1 = 2; // n 最后使用一块,剩下 n-1 块的写法
            int pre2 = 1; // n 最后使用两块,剩下 n-2 块的写法
            for (int i = 3; i <= target; i++){
                int cur = pre1 + pre2;
                pre2 = pre1;
                pre1 = cur;
            }
            return pre1; //相对于 n+1 块来说,第 n 种的方法
        }
    }

    2.4.3 位运算

     10 二进制中1的个数

    public class Solution {
        public int NumberOf1(int n) {
    
            int count = 0;
            while(n!=0)
            {
                n = n&(n-1);
                count++;
            }
            return count;
        }
    }

     

  • 相关阅读:
    php method_exists( $object , string $method_name )
    php伪类型 (mixed)
    6.6-2-数组与数据结构(用数组及其函数实现堆栈等数据结构)
    6.6-1-php数组相关(2)
    2017.6.5-2-php数组相关(1)
    2017.6.5-1-php函数应用及流程控制
    CodePage
    bat批处理教程
    pip安装及源
    CentOS安装Python3
  • 原文地址:https://www.cnblogs.com/coding-fairyland/p/12270223.html
Copyright © 2011-2022 走看看