剑指offer重点题6天刷完(day1)

day1

牛客 JZ1(LeetCode 04) 二维数组中的查找

时间复杂度:O(m+n);
空间复杂度:O(1)
public class Solution {
    public boolean Find(int target, int [][] array) {
        int startR = array.length-1;
        int startC = 0;
        while(startR>=0&&startC<=array[0].length-1){
            if(array[startR][startC]<target){
                startC++;
            }else if(array[startR][startC]>target){
                startR--;
            }else{
                return true;
            }
        }
        return false;
    }
}

从尾到头打印链表

时间复杂度:O(n)
空间复杂度:O(n)
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode==null){
            System.out.println(1);
            return new ArrayList<Integer>;
        }
        Stack<Integer> stack = new Stack<>();
        ArrayList<Integer> list = new ArrayList<>();
        while(listNode!=null){
            stack.push(listNode.val);
            
            listNode = listNode.next;
        }
        
        while(!stack.isEmpty()){
            list.add(stack.pop());
        }
        return list;
    }
}

重建二叉树

时间复杂度:O(N) : 其中 N 为树的节点数量。初始化 HashMap 需遍历 in ,占用 O(N) 。递归共建立 N 个节点,每层递归中的节点建立、搜索操作占用 O(1) ,因此使用 O(N) 时间。
空间复杂度:O(N)HashMap 使用 O(N) 额外空间。最差情况下,树退化为链表,递归深度达到 N ,占用 O(N) 额外空间;最好情况下,树为满二叉树,递归深度为 logN ,占用 O(logN) 额外空间。

import java.util.Map;
import java.util.HashMap;
public class Solution {
    private Map<Integer,Integer> map;
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        this.map = new HashMap<>();
        for (int i = 0; i < in.length; i++) {
            map.put(in[i], i);
        }


        return buildTreeMethod(pre,0,pre.length-1,
                               in,0,in.length-1);
    }
    
    public TreeNode buildTreeMethod(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn ){
        if(startPre>endPre||startIn>endIn){
            return null;
        }
        int index = map.get(pre[startPre]);
        TreeNode root = new TreeNode(pre[startPre]);
        int leftSize = index - startIn;

        TreeNode left = buildTreeMethod(pre,startPre+1,startPre+leftSize,
        in, startIn,index-1);
        root.left = left;
        TreeNode right = buildTreeMethod(pre,startPre+leftSize+1,endPre,
        in,index+1,endIn );
        root.right = right;

        return root;
    }
}

斐波那契数列

时间复杂度:O(n)
空间复杂度: O(1)
public class Solution {
    public int Fibonacci(int n) {
        //f(n) = f(n-1)+f(n-2)
        int a = 0;
        int b = 1;
        int res = 0;
        for(int i=1;i<=n;i++){
            res = a+b;
            b = a;
            a = res;
        }
        return res;
    }
}

青蛙跳台阶(与斐波那契一致)

public class Solution {
    public int jumpFloor(int target) {
        int a = 1, b = 1, sum;
        for(int i = 0; i < target; i++){
            sum = (a + b) % 1000000007;
            a = b;
            b = sum;
        }
        return a;
    }
}

变态跳台阶(不同的是每次可以跳从1到n,n阶台阶)

public class Solution {
    public int jumpFloorII(int target) {
        //易知 f(n)=f(n-1)+f(n-2)+……f(1)
        //f(n-1)=f(n-2)+……f(1)
        //两式相减得f(n)=2f(n-1)
        return 1<<target - 1;
    }
}

旋转数组的最小数字

时间复杂度:O(logn)
空间复杂度 :O1import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array==null||array.length==0){
            return 0;
        }
        int start = 0;
        int end = array.length-1;
        while(start<end){
            int mid = start + (end - start)/2;
            if(array[mid]<array[end]){
                end = mid;
            }else if(array[mid]>array[end]){
                //注意!这里start等于mid+1;start<end的二分法,start是mid+1或者end是mid-1(依据题目看选择哪种)
                //每次考虑缩短区间,与start+1<mid不同。
                start = mid+1;
            }else{
                end--;
            }
        }
        return array[start];
    }
}

用两个栈实现队列

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        this.dao(stack1,stack2);
        int res = stack2.pop();
        this.dao(stack2,stack1);
        return res;
    }
    
    public void dao(Stack<Integer> stack1,Stack<Integer> stack2){
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
    }
}

判断一个数有多少个1

public class Solution {
    public int NumberOf1(int n) {
         int res = 0;
        while(n!=0){
            //消去最后一个1
            n = n&n-1;
            res++;
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值