java基础编程题(2)
1、給定一個二叉樹,找出其最大深度。
注:二叉樹的深度為根節(jié)點(diǎn)到最遠(yuǎn)葉子節(jié)點(diǎn)的最長路徑上的節(jié)點(diǎn)數(shù)。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/ class Solution {public int maxDepth(TreeNode root) {return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;} }
?
2、給定一個僅包含?0 和 1 的二維二進(jìn)制矩陣,找出只包含 1 的最大矩形,并返回其面積。
?
?解題思路:
(1)首先求出高度是 1 的矩形面積,也就是它自身的數(shù),如圖中橙色的 4,面積就是 4。
(2)然后向上擴(kuò)展一行,高度增加一,選出當(dāng)前列最小的數(shù)字,作為矩陣的寬,求出面積,對應(yīng)上圖的矩形框。
(3)然后繼續(xù)向上擴(kuò)展,重復(fù)步驟 2。
?
?
?
?
?
?
?
?
?以此類推
?
class Solution {public int maximalRectangle(char[][] matrix) {if (matrix.length == 0) {return 0;}//保存以當(dāng)前數(shù)字結(jié)尾的連續(xù) 1 的個數(shù)int[][] width = new int[matrix.length][matrix[0].length];int maxArea = 0;//遍歷每一行for (int row = 0; row < matrix.length; row++) {for (int col = 0; col < matrix[0].length; col++) {//更新 widthif (matrix[row][col] == '1') {if (col == 0) {width[row][col] = 1;} else {width[row][col] = width[row][col - 1] + 1;}} else {width[row][col] = 0;}//記錄所有行中最小的數(shù)int minWidth = width[row][col];//向上擴(kuò)展行for (int up_row = row; up_row >= 0; up_row--) {int height = row - up_row + 1;//找最小的數(shù)作為矩陣的寬minWidth = Math.min(minWidth, width[up_row][col]);//更新面積maxArea = Math.max(maxArea, height * minWidth);}}}return maxArea;} }
?
3、找出數(shù)組中重復(fù)的數(shù)字
在一個長度為n的數(shù)組里的所有數(shù)字都在0到n-1的范圍內(nèi)。數(shù)組中某些數(shù)字是重復(fù)的,但不知道有幾個數(shù)字重復(fù)了,也不知道每個數(shù)字重復(fù)了幾次。請找出數(shù)組中任意一個重復(fù)的數(shù)字。例如,如果輸入長度為7的數(shù)組{2, 3, 1, 0, 2, 5, 3},那么對應(yīng)的輸出是重復(fù)的數(shù)字2或者3。
思路:從哈希表的思路拓展,重排數(shù)組:把掃描的每個數(shù)字(如數(shù)字m)放到其對應(yīng)下標(biāo)(m下標(biāo))的位置上,若同一位置有重復(fù),則說明該數(shù)字重復(fù)。
public static void main(String[] args) { //輸入數(shù)組的長度;Scanner sc = new Scanner(System.in);System.out.print("請輸入取值范圍(0~n):" + "\t");int n = sc.nextInt();//題目要求的數(shù)組ArrayList<Integer> arr = new ArrayList<>(); //去重數(shù)組HashSet<Integer> mset = new HashSet<>();Random ran = new Random(); //隨機(jī)n次,產(chǎn)生n個數(shù),存入數(shù)組中for (int i = 0; i < n; i++) { //產(chǎn)生一個0~n-1之間的隨機(jī)數(shù),存入數(shù)組int j = ran.nextInt(n);arr.add(j);mset.add(j);}System.out.println("------------------------[ 原始數(shù)據(jù) ]------------------------");System.out.println("隨機(jī)數(shù)組值為:" + arr);System.out.println("去重參考值為:" + mset); //------------------------------------------前期準(zhǔn)備工作-------------------------------------------------------//這時候,得到了一個符合題目要求的數(shù)組;和一個沒有重復(fù)的set集合if (arr.size() <= 0 || arr == null) {System.out.println("無效數(shù)組!");}if (arr.size() == mset.size()) {System.out.println("數(shù)組中無重復(fù)數(shù)字!");} else { //有重復(fù)數(shù)字,重復(fù)的數(shù)組為:arr數(shù)組減去無重復(fù)數(shù)組msetfor (Integer integer : mset) { //得到該數(shù)字再arr數(shù)組中第一次的索引,刪除int i = arr.indexOf(integer);arr.remove(i);}//再去重HashSet<Integer> list = new HashSet<>();for (Integer i : arr) {list.add(i);} //重復(fù)數(shù)字為:System.out.println("重復(fù)的值為 : " + list);}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/strong-FE/p/11470065.html
總結(jié)
以上是生活随笔為你收集整理的java基础编程题(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个和杨洋有关的qq网名
- 下一篇: 广州削骨一般要多少钱?