牛客网面试高频题top100(21~30)

面试高频算法题top100(21~30)java实现

21.按之字形顺序打印二叉树

给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)

import java.util.*;

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

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

    }

}
*/
public class Solution {
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        Queue<TreeNode> que = new LinkedList<>();
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if(pRoot==null) return res;
        que.add(pRoot);
        int count = 0;
        
        while(!que.isEmpty()){
            int len = que.size();
            ArrayList<Integer> list = new ArrayList<>();
            for(int i=0;i<len;i++){
                TreeNode temp = que.poll();
                if(count%2!=0)
                    list.add(0,temp.val);
                else
                    list.add(temp.val);
                if(temp.left!=null)
                    que.add(temp.left);
                if(temp.right!=null)
                    que.add(temp.right);
            }
            res.add(list);
            count += 1;
        }
        return res;
    }

}

22.最长公共子串

给定两个字符串str1和str2,输出两个字符串的最长公共子串
题目保证str1和str2的最长公共子串存在且唯一。

import java.util.*;


public class Solution {
    /**
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     */
    public String LCS (String str1, String str2) {
        String str = "";
        int i=0,end=1;
        while(end<=str2.length()){
            String temp = str2.substring(i,end);
            if(str1.indexOf(temp)!= -1){
                 str = str.length()<temp.length()?temp:str;
            }else{
                i ++;
            }
              end++;  
        }
        return str;
    }
}

23.两个链表的第一个公共结点

输入两个无环的单向链表,找出它们的第一个公共结点,如果没有公共节点则返回空。
在这里插入图片描述

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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        int l1 = 0;
        int l2 = 0;
        ListNode H1 = pHead1;
        ListNode H2 = pHead2;
        while(pHead1!=null){
            l1 ++;
            pHead1 = pHead1.next;
        }
        while(pHead2!=null){
            l2 ++;
            pHead2 = pHead2.next;
        }
        int temp = l1>=l2?l1-l2:l2-l1;
        while(temp!=0){
            if(l1>=l2){
                H1 = H1.next;
            }else{
                H2 = H2.next;
            }
            temp --;
        }
        while(H1!=null){
            if(H1==H2){
                break;
            }
            H1= H1.next;
            H2 = H2.next;
        }
        return H1;
    }
}

24.链表相加

在这里插入图片描述

import java.util.*;

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

public class Solution {
    public ListNode addInList (ListNode head1, ListNode head2) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while(head1!=null){
            stack1.push(head1.val);
            head1 = head1.next;
        }
         while(head2!=null){
            stack2.push(head2.val);
            head2 = head2.next;
        }   
        int more = 0;
        ListNode head = null;
        ListNode pre = null;
        while(!stack1.empty() || !stack2.empty() || more!=0){
            int s1 = stack1.empty()?0:stack1.pop();
            int s2 = stack2.empty()?0:stack2.pop();
             more += s1+s2;
            pre = new ListNode(more%10);
            pre.next = head;
            head = pre;
            more /= 10;
        }
        
        return head;
    }
}

25.在二叉树中找到两个节点最近的公共祖先

给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点。
在这里插入图片描述
所以节点值为5和节点值为1的节点的最近公共祖先节点的节点值为3,所以对应的输出为3。(节点本身可以视为自己的祖先)

import java.util.*;

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

public class Solution {
    public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
        TreeNode res = search(root,o1,o2);
        return res.val;
    }
    public TreeNode search(TreeNode root, int o1, int o2){
        if(root==null) return null;
        if(root.val==o1 || root.val==o2) return root;
        TreeNode left = search(root.left,o1,o2);
        TreeNode right = search(root.right,o1,o2);
        if(left!=null && right!=null) return root;
        return left!=null?left:right;
    }
}

26.反转字符串

写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。

import java.util.*;


public class Solution {
    public String solve (String str) {
        char[] cs = str.toCharArray();
        int i = 0,j = str.length()-1;
        while(i<j){
            char temp = cs[i];
            cs[i] = cs[j];
            cs[j] = temp;
            i++;
            j--;
        }
        return new String(cs);
    }
}

27.螺旋矩阵

给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。
在这里插入图片描述

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();
        if(matrix.length==0) return list;
        int up = 0,left = 0,down = matrix.length-1,right = matrix[0].length-1;
        //int i=0,j=0;
        while(true){
            for(int i=left;i<=right;i++) list.add(matrix[up][i]);
            up++;
            if(up>down) break;
            for(int j=up;j<=down;j++) list.add(matrix[j][right]);
            right--;
            if(left>right) break;
            for(int i=right;i>=left;i--) list.add(matrix[down][i]);
            down--;
            if(up>down) break;
            for(int j=down;j>=up;j--) list.add(matrix[j][left]);
            left++;
            if(left>right) break;
        }
        return list;
    }
}

28.斐波那契数列

在这里插入图片描述

public class Solution {
    public int Fibonacci(int n) {
        int f1 = 1,f2 = 1,f = 0;
        if(n<3) return 1;
        for(int i=3;i<=n;i++){
            f = f1+f2;
            f1 = f2;
            f2 = f;
        }
        return f;
    }
}

29.最长回文子串

对于长度为n的一个字符串A(仅包含数字,大小写英文字母),请设计一个高效算法,计算其中最长回文子串的长度。

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param A string字符串 
     * @return int整型
     */
    public int getLongestPalindrome (String A) {
        int max = 1;
        int i=0;
        char[] cs = A.toCharArray();
        while(i<cs.length){
            int temp = Math.max(judge(cs,i,i+1),judge(cs,i-1,i+1));
            max = Math.max(temp,max);
            i++;
        }
        return max;
    }
    public int judge(char[] cs,int left,int right){
        int max=0;
        while(left>=0 && right<cs.length && cs[left]==cs[right]){
                max = Math.max(max,right-left+1);
                left--;
                right++;
            }
        return max;
    }
}

30.三数之和

给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组。
在这里插入图片描述

import java.util.*;
public class Solution {
    public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if(num.length<3) return res;
        Arrays.sort(num);
        for(int i=0;i<num.length;i++){
            if(i!=0 && num[i]==num[i-1]) continue;
            if(num[i]>0) break;
            for(int j=i+1;j<num.length;j++){
                int temp = -num[i]-num[j];
                if(temp<0) break;
                if(Arrays.binarySearch(Arrays.copyOfRange(num,j+1,num.length),temp)>=0){
                    ArrayList<Integer> list = new ArrayList<>();
                    list.add(num[i]);
                    list.add(num[j]);
                    list.add(temp);
                    if(!res.contains(list))
                        res.add(list);
                }
            }
        }
        return res;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值