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);
}
}