高阶面试-其他算法

最近面美团,结果栽到了算法上,好不甘心,需要把各个大厂近半年的全部刷N遍,保证在O(1)的时间内作出来,希望接下来的一两周不再害怕常规的算法题

2980. 检查按位或是否存在尾随零

class Solution {
    public boolean hasTrailingZeros(int[] nums) {
        Set<String> binarySet = new HashSet<>();
        for(int num: nums){
            String curr = Integer.toBinaryString(num);
            for(String binary: binarySet){
                if(Integer.toBinaryString(num | Integer.parseInt(binary,2)).endsWith("0")){
                    return true;
                }
            }
            binarySet.add(curr);
        }
        return false;
    }
}

35. 搜索插入位置

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length -1;
        while(left <= right){
            int mid = left+ (right-left)/2;
            if(nums[mid] == target){
                return mid;
            }else if(nums[mid] > target){
                right = mid -1;
            }else {
                left = mid +1;
            }
        }
        return left;
    }
}   

48. 旋转图像

class Solution {
    public void rotate(int[][] matrix) {
        int n = matrix.length;

        // 对角线
        for(int i=0; i<n; i++){
            for(int j=i;j <n; j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        // 对每一行反转
        for(int i=0; i<n; i++){
            for(int j=0;j<n/2;j++){
                int temp = matrix[i][j];
                // 0,1,2,3
                matrix[i][j] = matrix[i][n-j-1];
                matrix[i][n-j-1] = temp;
            }
        }
    }
}

42. 接雨水

class Solution {
    public int trap(int[] height) {
        //双指针
        int left = 0;
        int right = height.length-1;
        int leftMax = 0;
        int rightMax = 0;
        int res = 0;

        while(left < right){
            leftMax = Math.max(height[left],leftMax);
            rightMax = Math.max(height[right],rightMax);

            // 走下坡路才需要
            if(height[left] < height[right]){
                res += leftMax-height[left];
                left++;
            }else{
                res += rightMax-height[right];
                right--;
            }
        }
        return res;
    }
}

70. 爬楼梯

class Solution {
    public int climbStairs(int n) {
        // check
        if(n <= 1){
            return 1;
        }

        int[] dp = new int[n+1];
        dp[1] = 1;
        dp[0] = 1;
        for(int i=2;i<=n;i++){
            dp[i] = dp[i-1]+dp[i-2];
        }
        return dp[n];
    }
}

82. 删除排序链表中的重复元素 II

/**
 * 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 deleteDuplicates(ListNode head) {
        // check
        if(head == null){
            return head;
        }

        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode prev = dummy;
        ListNode curr = head;
        while(curr != null){
            while(curr.next != null && curr.val == curr.next.val){
                curr = curr.next;
            }
            if(prev.next == curr){
                prev = prev.next;
            }else{
                // 重复
                prev.next = curr.next;
            }
            curr = curr.next;
        }
        return dummy.next;
    }
}

206. 反转链表

/**
 * 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 reverseList(ListNode head) {
        // check
        if(head == null || head.next == null){
            return head;
        }
        
        ListNode prev = null;
        ListNode curr = head;
        while(curr != null){
            // 1->2
            ListNode tempNext = curr.next;
            curr.next = prev;
            prev = curr;
            curr = tempNext;
        }
        return prev;
    }
}

958. 二叉树的完全性检验

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isCompleteTree(TreeNode root) {
        // check
        if(root == null){
            return true;
        }

        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        boolean isEnd = false;
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            if(isEnd && node!= null){
                return false;
            }
            if(node == null){
                isEnd = true;
                continue;
            }
            queue.offer(node.left);
            queue.offer(node.right);
        }
        return true;
    }
}

LCR 152. 验证二叉搜索树的后序遍历序列

class Solution {
    public boolean verifyTreeOrder(int[] postorder) {
        // 左右中
        return recur(postorder,0,postorder.length-1);
    }

    public boolean recur(int[] postorder, int start, int end){
        // terminal
        if(start >= end){
            return true;
        }
        int root = postorder[end];
        int mid = start;
        // left right的分界点
        while(mid < end && postorder[mid] < root){
            mid++;
        }
        // 检查 right 是否都 > root
        for(int i=mid; i<end;i++){
            if(postorder[i] < root){
                return false;
            }
        }
        return recur(postorder,start,mid-1) && recur(postorder,mid,end-1);
    }
}

LCR 016. 无重复字符的最长子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        //滑动窗口
        // hash 字符:索引
        HashMap<Character,Integer> map = new HashMap<>();
        int maxLength = 0;
        int left = 0;

        for(int right = 0;right<s.length();right++){
            char currentChar = s.charAt(right);
            //如果已存在,更新left
            if(map.containsKey(currentChar)){
                left = Math.max(map.get(currentChar)+1,left);
            }
            // 更新hash表和最大长度
            map.put(currentChar,right);
            maxLength = Math.max(maxLength,right-left+1);
        }
        return maxLength;
    }
}

LCR 026. 重排链表

/**
 * 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 void reorderList(ListNode head) {
        if(head == null || head.next == null){
            return;
        }

        Stack<ListNode> stack = new Stack<>();
        ListNode curr = head;
        while(curr != null){
            stack.push(curr);
            curr = curr.next;
        }

        int n = stack.size();
        curr = head;
        for(int i=0;i<n/2;i++){
            ListNode temp = curr.next;
            ListNode top = stack.pop();
            curr.next = top;
            top.next = temp;
            curr = curr.next.next
        }
        curr.next = null;
    }
}

LCR 024. 反转链表

/**
 * 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 reverseList(ListNode head) {
        // check
        if(head == null || head.next == null){
            return head;
        }
        ListNode prev = null;
        ListNode curr = head;
        while(curr != null){
            // 1->2->3
            ListNode tempNext = curr.next;
            curr.next = prev;
            prev = curr;
            curr = tempNext;
        }
        // 最终 prev 
        return prev;
    }
}

3. 无重复字符的最长子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // check
        if(s==null || s.length()==0){
            return 0;
        }

        // 滑动窗口
        HashSet<Character> set = new HashSet<>();
        int maxLength = 0;
        int left = 0;
        int right =0;
        while(right < s.length()){
            while(set.contains(s.charAt(right))){
                set.remove(s.charAt(left));
                left++;
            }
            set.add(s.charAt(right));
            maxLength = Math.max(maxLength,right-left+1);
            right++;
        }
        return maxLength;
    }
}

5. 最长回文子串

中心扩展法的思想

回文字符串有一个对称的中心。我们可以从每个字符以及每两个字符之间作为中心开始,向两边扩展,看看能扩展多长的回文。

举个例子

假设我们有一个字符串“babad”。

我们从第一个字符‘b’开始:

  1. 以第一个‘b’作为中心,我们得到的回文是“b”。
  2. 以第一个‘b’和第二个‘a’之间作为中心,我们得到的回文长度是0(因为‘b’和‘a’不相同)。

然后我们以第二个字符‘a’开始:

  1. 以第二个‘a’作为中心,向两边扩展,得到的回文是“aba”。
  2. 以第二个‘a’和第三个‘b’之间作为中心,我们得到的回文长度是0(因为‘a’和‘b’不相同)。

我们继续这样做,直到遍历完所有字符。

代码

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // check
        if(s==null || s.length()==0){
            return 0;
        }

        // 滑动窗口
        HashSet<Character> set = new HashSet<>();
        int maxLength = 0;
        int left = 0;
        int right =0;
        while(right < s.length()){
            while(set.contains(s.charAt(right))){
                set.remove(s.charAt(left));
                left++;
            }
            set.add(s.charAt(right));
            maxLength = Math.max(maxLength,right-left+1);
            right++;
        }
        return maxLength;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值