为了账号安全,请及时绑定邮箱和手机立即绑定

2023程序员算法与数据结构面试攻略

难度初级
时长 2小时 7分
学习人数
综合评分8.40
5人评价 查看评价
9.2 内容实用
8.4 简洁易懂
7.6 逻辑清晰
  • 不存在会返回比目标数字大的,因为判断当前mid位置的数字<num时,最后一次left = mid + 1,判断当前mide位置的数字>num时,right = mide,所以是大于目标数字。

    最靠右的:

    function binarySearch(num, nums) {
        let left = 0, right = nums.length - 1;
        while(true) {
            if (left == right) {
            return left;
            }
            let mid = left + Math.floor((right - left) / 2);
            if (nums[mid] < num) {
                left = mid + 1;
            } else if (nums[mid] == num && nums[mid + 1] == num) {
                left = mid+1;
            } else {
                right = mid;
            }
        }
    }
    查看全部
  • class LRUCache {
        private int capacity;
        private int n;
        private DoubleLinkedList pHead,pTail;
        private DoubleLinkedList[] hash;
    
        private class DoubleLinkedList{
            int key, val;
            DoubleLinkedList prev, next;
    
            public DoubleLinkedList(int key, int val){
                this.key = key;
                this.val = val;
                this.prev = null;
                this.next = null;
            }
        }
    
        public LRUCache(int capacity) {
            this.capacity = capacity;
            this.n = 0;
            hash = new DoubleLinkedList[10001];
            pHead = new DoubleLinkedList(-1,0);
            pTail = new DoubleLinkedList(-2,0);
            pHead.next = pTail;
            pTail.prev = pHead;
        }
        
        public int get(int key) {
            DoubleLinkedList node = hash[key];
            if(node==null){
                return -1;
            }
            moveFront(node);
            return node.val;
        }
        
        public void put(int key, int value) {
            DoubleLinkedList node = hash[key];
            if(node == null && n < capacity){
                node = new DoubleLinkedList(key,value);
                hash[key] = node;
                addFront(node);
                n++;
                return;
            }
            if(node ==null && n==capacity){
                node = pTail.prev;
                hash[node.key] = null;
                hash[key] = node;
            }
            node.key = key;
            node.val = value;
            moveFront(node);
        }
    
        private void moveFront(DoubleLinkedList node){
            node.prev.next = node.next;
            node.next.prev = node.prev;
            addFront(node);
        }
    
        private void addFront(DoubleLinkedList node){
            node.prev = pHead;
            node.next = pHead.next;
            pHead.next.prev = node;
            pHead.next = node;
        }
    }
    
    /**
     * Your LRUCache object will be instantiated and called as such:
     * LRUCache obj = new LRUCache(capacity);
     * int param_1 = obj.get(key);
     * obj.put(key,value);
     */
    查看全部
    0 采集 收起 来源:LRU缓存实战

    2023-03-22

  • /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            int count = 0;
            ListNode current = head;
            ListNode previous = null;
            ListNode newCurrent  = current;
            ListNode leftBreak = null,reverseTail = head, reverseHead = null;
            while(true){
                count ++;
                if(count == k){
                    reverseHead = current;
                    current = reverseTail;
                    previous = null;
                    while (previous != reverseHead){
                        newCurrent = current.next;
                        current.next = previous;
                        previous = current;
                        current = newCurrent;
                    }
                    if(leftBreak == null){
                        head = reverseHead;
                    }else{
                        leftBreak.next = reverseHead;
                    }
                    leftBreak = reverseTail;
                    reverseTail.next = current;
                    reverseTail = current;
                    count = 0;
                }else{
                    current = current.next;
                }
                if(current == null){
                    break;
                }
    
            }
            return head;
        }
    }
    查看全部
    0 采集 收起 来源:反转链表实战

    2023-03-21

    1. 二分

    2. 2.考点:

    查看全部

举报

0/150
提交
取消
课程须知
适合建议掌握一门高级语言基础(Java优先),且正在备战算法面试,或有意了解大厂技术面试的同学。
老师告诉你能学到什么?
1、面试官角度展示考查内容和考察逻辑。 2、拆分常考点,提示易错点,提升复习效率。 3、解读面试难度上升路线,助力多层次备战。

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!