剑指Offer行榜【牛客网】练习(八)

28 篇文章 0 订阅
13 篇文章 0 订阅
1、两个链表的第一个公共结点

题目描述:
输入两个链表,找出它们的第一个公共结点。

思路:
用两张ArrayList依次保存两个链表的值,如果当前结点在另一个ArrayList中出现,则是公共结点。

代码:

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

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.ArrayList;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ArrayList<Integer> val1 = new ArrayList<>();
        ArrayList<Integer> val2 = new ArrayList<>();
        while(true){
            if(pHead1==null&&pHead2==null){
                return null;
            }else{
                if(pHead1!=null){
                    if(val2.indexOf(pHead1.val)<0){
                        val1.add(pHead1.val);
                        pHead1 = pHead1.next;
                    }else{
                        return pHead1;
                    }
                }
                if(pHead2!=null){
                    if(pHead2!=null&&val1.indexOf(pHead2.val)<0){
                        val2.add(pHead2.val);
                        pHead2 = pHead2.next;
                    }else{
                        return pHead2;
                    }
                }
            }
            
        }
    }
}
2、统计一个数字在数组中出现的次数

题目描述:
统计一个数字在排序数组中出现的次数。

代码:

public class Solution {
    public int GetNumberOfK(int [] array , int k) {
       int count = 0;
        for(int i=0;i<array.length;i++){
            if(k==array[i]){
                count++;
            }
        }
        return count;
    }
}
3、二叉树的深度

题目描述:
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

思路:
利用递归来计算每一条路径的长度,然后排序即可。

代码:

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

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

    }

}
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
    
    ArrayList<Integer> depths = new ArrayList<>();
    
    public int TreeDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        calculateDepth(root,0);
        Collections.sort(depths);
        return depths.get(depths.size()-1);
    }
    
    public void calculateDepth(TreeNode root, int depth){
        if(root!=null){
            depth++;
            calculateDepth(root.left,depth);
            calculateDepth(root.right,depth);
        }else{
            depths.add(new Integer(depth));
            return;
        }
    }
}
4、平衡二叉树

题目描述:
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

思路:
利用上一题求深度的代码,比较每一结点左右最深深度的差,若有一结点左右深度差大于1则不是平衡二叉树。

代码:

import java.util.ArrayList;
import java.util.Collections;
public class Solution {
    
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null){
            return true;
        }
        return judge(root);
    }
    
    public boolean judge(TreeNode root){
        if(root.left!=null&&root.right!=null){
            if(Math.abs(TreeDepth(root.left)-TreeDepth(root.right))>1){
                return false;
            }else{
                return judge(root.left)&&judge(root.right);
            }
        }else if(root.left!=null){
            if(TreeDepth(root.left)>1){
                return false;
            }else{
                return true;
            }
        }else if(root.right!=null){
            if(TreeDepth(root.right)>1){
                return false;
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
    
    public int TreeDepth(TreeNode root) {
        ArrayList<Integer> depths = new ArrayList<>();
        calculateDepth(root,0,depths);
        Collections.sort(depths);
        return depths.get(depths.size()-1);
    }
    
    public void calculateDepth(TreeNode root, int depth, ArrayList<Integer> depths){
        if(root!=null){
            depth++;
            calculateDepth(root.left,depth,depths);
            calculateDepth(root.right,depth,depths);
        }else{
            depths.add(new Integer(depth));
            return;
        }
    }
}
5.数组中值出现一次的数字

题目描述:
一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。

思路:
依次统计出现的数字的数字即可。

代码:

//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
import java.util.ArrayList;
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        ArrayList<Integer> nums = new ArrayList<>();
        ArrayList<Integer> counts = new ArrayList<>();
        for(int i=0;i<array.length;i++){
            int index = nums.indexOf(array[i]);
            if(index<0){
                nums.add(array[i]);
                counts.add(1);
            }else{
                int count = counts.get(index)+1;
                counts.remove(index);
                counts.add(index,new Integer(count));
            }
        }
        int[] result = new int[2];
        int j = 0;
        for(int i=0;i<counts.size();i++){
            if(counts.get(i)==1){
                result[j] = i;
                j++;
            }
        }
        num1[0] = nums.get(result[0]);
        num2[0] = nums.get(result[1]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值