剑指Offer【牛客网】刷题(八)

36、两个链表的第一个公共结点

题目描述:输入两个链表,找出它们的第一个公共结点。
解题思路:遍历两个链表,找到值相同的结点,验证是否是公共结点(公共结点的含义是这两个结点后继结点也都指向同样的结点),有公共结点的两条链表的拓扑结构应该类似Y而不是X

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

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode p2 = pHead2;
        while(pHead1 != null) {
            while(p2 != null) {
                if(pHead1.val == p2.val){
                    boolean isCommon = isCommon(pHead1, p2);
                    if(isCommon) {
                        return p2;
                    }
                }
                p2 = p2.next;
            }
            pHead1 = pHead1.next;
            p2 = pHead2;
        }
        return null;
    }
    
    public boolean isCommon(ListNode node1, ListNode node2) {
        
        while(node1 != null || node2 != null) {
            if(node1.val == node2.val) {
                node1 = node1.next;
                node2 = node2.next;
            }else {
                return false;
            }
        }
        
        return true;
    }
}

37、数字在排序数组中出现的次数

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

public class Solution {
    public int GetNumberOfK(int [] array , int k) {
       if(array == null || array.length == 0) {
           return 0;
       }
        
        int count = 0;
        for(int i = 0; i < array.length; i++) {
            if(array[i] == k) {
                count ++;
            }
        }
        return count;
    }
}

38、二叉树的深度

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

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

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

    }

}
*/
public class Solution {
    int m_depth = 1;

    public int TreeDepth(TreeNode root) {
        
        if(root == null) {
            return 0;
        }
        
        getDepth(root, 1);

        return m_depth;
    }
    
    public void getDepth(TreeNode node, int depth) {
        
        if(node.left != null) {
            getDepth(node.left, depth+1);
        }
        
        if(node.right != null) {
            getDepth(node.right, depth+1);
        }
        
        m_depth = Math.max(depth, m_depth);
        
        //return 0;
    }
}

39、平衡二叉树

题目描述:输入一棵二叉树,判断该二叉树是否是平衡二叉树。
解题思路:复用了上一题求二叉树深度的代码,求左右子树的深度作差。一次判断是否平衡。

public class Solution {
    
    int m_depth = 1;
    //boolean result = false;
    public boolean IsBalanced_Solution(TreeNode root) {
        
        if(root == null) {
            return true;
        }

        int l = 0;
        int r = 0;
        if(root.left != null) {
            m_depth = 1;
            l = TreeDepth(root.left);
        }

        if(root.right != null) {
            m_depth = 1;
            r = TreeDepth(root.right);
        }
        
        boolean is_left = true;
        boolean is_right = true;

        if(Math.abs(l-r) <= 1) {
            if(root.left != null) {
                is_left =  IsBalanced_Solution(root.left);
            }
            
            if(root.right != null) {
                is_right = IsBalanced_Solution(root.right);
            }
            
            return is_left && is_right;
        }else {
            return false;
        }

        //return false;
    }
    
    

    public int TreeDepth(TreeNode root) {
        
        if(root == null) {
            return 0;
        }
        
        getDepth(root, 1);

        return m_depth;
    }
    
    public void getDepth(TreeNode node, int depth) {
        
        if(node.left != null) {
            getDepth(node.left, depth+1);
        }
        
        if(node.right != null) {
            getDepth(node.right, depth+1);
        }
        
        m_depth = Math.max(depth, m_depth);
        
        //return 0;
    }
}

40、数组中只出现一次的数

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

//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> list = new ArrayList<>();
        ArrayList<Integer> indexs = new ArrayList<>();
        
        for(Integer i: array) {
            int index = list.indexOf(i);
            if(index < 0) {
                list.add(i);
                indexs.add(1);
            }else {
                int num = indexs.get(index);
                num++;
                indexs.remove(index);
                indexs.add(index, num);
            }
        }
        
        ArrayList<Integer> result = new ArrayList<>();
        for(int i = 0; i < indexs.size(); i++) {
            if(indexs.get(i) == 1) {
                result.add(list.get(i));
            }
        }
        num1[0] = result.get(0);
        num2[0] = result.get(1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值