二叉树的遍历:递归,非递归 public class Tree { public class Node{ public Node left; public Node right; public int val; public Node(int val) { this.val = val; } } //前序递归 public void preOrderRecur(Node he...
编程题:二叉树的最大距离 import java.util.*;public class Main{ public static class TreeNode{ TreeNode left; TreeNode right; int val; public TreeNode(int val){ ...
编程题:把二叉树打印成多行(非递归) import java.util.*;/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class...
编程题:把二叉树打印成多层(递归) import java.util.ArrayList;/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/publ...
编程题:链表中的环 /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode EntryNodeOfLoop(ListNode pHe...
编程题:数组中只出现一次的数 题目描述一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。//num1,num2分别为长度为1的数组。传出参数//将num1[0],num2[0]设置为返回结果//先用亦或,相同则为0,不同则为1,最后得到的就是单独的2个数的亦或,是2个数不同的位置,//选第一个1的位置,将数组分为2组,则2个数分到不同组,相同的数分到相同组,则可以在分好的组...
编程题:平衡二叉树 题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。public class Solution { public boolean IsBalanced_Solution(TreeNode root) { if(root == null){ return true; } return IsBalanced_Solu...
编程题:树的深度 题目描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。public class Solution { public int TreeDepth(TreeNode root) { if(root == null){ return 0; } ...
编程题:正则表达式匹配 请实现一个函数用来匹配包括’.‘和’‘的正则表达式。模式中的字符’.‘表示任意一个字符,而’'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配public class Solution { public boolean match(char[]...
编程题:表示数值的字符串 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。用正则public class Solution { public boolean isNumeric(char[] str) { ...
编程题:287. Find the Duplicate Number Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi...
编程题:56. Merge Intervals Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, m...
编程题:17. Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is give...
编程题:55. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you ...
编程题:3sum,乱序数组 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contai...
编程题:getMin栈 题目描述:实现一个特殊功能的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。CODEimport java.util.*;public class Main{ public static void main(String[]args){ getMinStack ms = new getMinStack(); Scanner scanner = new...
编程题:用两个栈实现队列,支持队列的基本操作 题目要求:用两个栈实现队列,支持队列的基本操作CODEimport java.util.*;public class Main{ public static void main(String[] args){ QueueByStack sq = new QueueByStack(); Scanner scanner = new Scann...
编程题:不重复3SUM 题目描述给定排序数组arr和整数k,不重复打印arr中所有相加和为k的不降序三元组例如, arr = [-8, -4, -3, 0, 1, 2, 4, 5, 8, 9], k = 10,打印结果为:-4 5 9-3 4 9-3 5 80 1 90 2 81 4 5[要求]时间复杂度为O(n^2)空间复杂度为O(1)输入描述:第一行有两个整数n, k接下来一行有n个整数表...
编程题:不重复2sum 题目描述给定排序数组arr和整数k,不重复打印arr中所有相加和为k的不降序二元组例如, arr = [-8, -4, -3, 0, 1, 2, 4, 5, 8, 9], k = 10,打印结果为:1, 92, 8[要求]时间复杂度为O(n),空间复杂度为O(1)输入描述:第一行有两个整数n, k接下来一行有n个整数表示数组内的元素输出描述:输出若干行,每行两个整数表示答案...
编程题:最长可整合子数组 题目描述先给出可整合数组的定义:如果一个数组在排序之后,每相邻两个数的差的绝对值都为1,或者该数组长度为1,则该数组为可整合数组。例如,[5, 3, 4, 6, 2]排序后为[2, 3, 4, 5, 6],符合每相邻两个数差的绝对值都为1,所以这个数组为可整合数组给定一个数组arr, 请返回其中最大可整合子数组的长度。例如,[5, 5, 3, 2, 6, 4, 3]的最大可整合子数组为[5,...