牛客网面试高频题top100(61~70)

这篇博客包含了7道使用Java实现的算法题目,包括:有序链表去重、字符串转整数、数字反转、矩阵元素查找、缺失正整数、链表奇偶重排、二叉树最大路径和、对称二叉树检查以及括号生成。这些题目涵盖了数据结构和算法的基础知识,适合面试准备或提升编程能力。
摘要由CSDN通过智能技术生成

面试高频算法题top100(61~70)java实现

61.删除有序链表中重复的元素(二)

在这里插入图片描述

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        while(head!=null){
            ListNode next = head.next;
            if(next==null) break;
            if(head.val==next.val){
                while(next!=null && head.val==next.val)
                    next = next.next;
                pre.next = next;
                head = next;
            }else{
                pre = head;
                head = head.next;
            }
        }
        return dummy.next;
    }
}

62.把字符串转换成整数

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import java.util.*;


public class Solution {
    
    public int StrToInt (String s) {
        s = s.trim();
        if(s.length()<1) return 0;
        int i=0;
        int sign = 1;
        long num=0;
        if(s.charAt(i)=='+')
            i++;
        else if(s.charAt(i)=='-'){
            sign = -1;
            i++;
        }
        while(i<s.length()){
            if(Character.isDigit(s.charAt(i))){
                while(i<s.length() && Character.isDigit(s.charAt(i))){
                    num = num*10 + s.charAt(i)-'0';
                    if(sign==1 && num>=Integer.MAX_VALUE){
                        return Integer.MAX_VALUE;
                    }else if(sign==-1 && -num<=Integer.MIN_VALUE)
                        return Integer.MIN_VALUE;
                    i++;
                }
            }else
                break;
        }
        return (int)num*sign;
    }
}

63.反转数字

在这里插入图片描述

import java.util.*;


public class Solution {
    
    public int reverse (int x) {
        int num=0,temp=0;
        while(x!=0){
            num = temp*10 + x%10;
            if((num-x%10)/10!=temp)
                    return 0;
            temp = num;
            x /= 10;
        }
        return num;
    }
    
}

64.矩阵元素查找

已知一个有序矩阵mat,同时给定矩阵的大小n和m以及需要查找的元素x,且矩阵的行和列都是从小到大有序的。设计查找算法返回所查找元素的二元数组,代表该元素的行号和列号(均从零开始)。保证元素互异。
在这里插入图片描述

import java.util.*;

public class Solution {
    public int[] findElement(int[][] mat, int n, int m, int x) {
        int i=n-1,j=0;
        int[] res = new int[2];
        if(n==0 || m==0) return res;
        while(i>=0 && j<m){
            if(mat[i][j]==x){
                res[0] = i;
                res[1] = j;
                break;
            }else if(mat[i][j]>x)
                i--;
            else
                j++;
        }
        return res;
    }
}

65.缺失的第一个正整数

给定一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数
在这里插入图片描述

import java.util.*;


public class Solution {
    public int minNumberDisappeared (int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i:nums)
            map.put(i,1);
        int j=1;
        while(j<=nums.length+1){
            if(map.getOrDefault(j,0)>0)
                j++;
            else
                break;
        }
        return j;
    }
}

空间复杂度优化为O(1):

import java.util.*;
public class Solution {
    public int minNumberDisappeared (int[] nums) {
        int n = nums.length;
        //遍历数组
        for(int i = 0; i < n; i++) 
            //负数全部记为n+1
            if(nums[i] <= 0) 
                nums[i] = n + 1;
        for(int i = 0; i < n; i++)
            //对于1-n中的数字
            if(Math.abs(nums[i]) <= n) 
                //这个数字的下标标记为负数
                nums[Math.abs(nums[i]) - 1] = -1 * Math.abs(nums[Math.abs(nums[i]) - 1]); 
        for(int i = 0; i < n; i++)
            //找到第一个元素不为负数的下标
            if(nums[i] > 0)
                return i + 1;
        return n + 1;
    }
}

66.链表的奇偶重排

给定一个单链表,请设定一个函数,将链表的奇数位节点和偶数位节点分别放在一起,重排后输出。
注意是节点的编号而非节点的数值。
在这里插入图片描述

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    
    public ListNode oddEvenList (ListNode head) {
        if(head==null) return null;
        ListNode J = head;
        ListNode res = J;
        head = head.next;
        if(head==null) return J;
        ListNode O = head;
        ListNode temp = O;
        head = head.next;
        int count = 3;
        while(head!=null){
            if(count%2!=0){
                J.next = head;
                J = J.next;
            }else{
                O.next = head;
                O = O.next;
            }
            count ++;
            head = head.next;
        }
        J.next = temp;
        O.next = null;
        return res;
    }
}

67.二叉树中的最大路径和

二叉树里面的路径被定义为:从该树的任意节点出发,经过父=>子或者子=>父的连接,达到任意节点的序列。
注意:
1.同一个节点在一条二叉树路径里中最多出现一次
2.一条路径至少包含一个节点,且不一定经过根节点
在这里插入图片描述
在这里插入图片描述

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    int sum = Integer.MIN_VALUE;
    public int maxPathSum (TreeNode root) {
        search(root);
        return sum;
    }
    public int search(TreeNode root){
        if(root==null) return 0;
        int left1 = Math.max(0,search(root.left));
        int right1  = Math.max(0,search(root.right));
        int temp = root.val+left1+right1;
        sum = Math.max(temp,sum);
        return Math.max(root.val+left1,root.val+right1);
    }
}

68.对称的二叉树

在这里插入图片描述
在这里插入图片描述

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.*;
public class Solution {
    boolean isSymmetrical(TreeNode pRoot) {
        if(pRoot==null)
            return true;
       Queue<TreeNode> que = new LinkedList<>();
        que.add(pRoot);
        que.add(pRoot);
        while(!que.isEmpty()){
            TreeNode t1 = que.poll();
            TreeNode t2 = que.poll();
            if(t1==null && t2==null)
                continue;
            if(t1==null || t2==null)
                return false;
            if(t1.val!= t2.val)
                return false;
            que.add(t1.left);
            que.add(t2.right);
            que.add(t1.right);
            que.add(t2.left);
        }
        return true;
    }
}

69.括号生成

在这里插入图片描述

import java.util.*;


public class Solution {
    ArrayList<String> res = new ArrayList<>();
    public ArrayList<String> generateParenthesis (int n) {
        deal(n,0,0,"");
        return res;
    }
    public void deal(int n,int left,int right,String str){
        if(left==n && right==n) {
            res.add(str);
            return;
        }
        if(left<n)
            deal(n,left+1,right,str+'(');
        if(right<left)
            deal(n,left,right+1,str+')');
        
    }
}

70.顺时针旋转矩阵

有一个nxn整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个nxn的矩阵,和矩阵的阶数n,请返回旋转后的nxn矩阵。
在这里插入图片描述
空间复杂度优化:

import java.util.*;

public class Solution {
    public int[][] rotateMatrix(int[][] mat, int n) {
        int[][] res = new int[n][n];
        for(int j=0;j<n;j++){
            for(int i=n-1;i>=0;i--)
                res[j][n-i-1] = mat[i][j];
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值