原创 [劍指offer] 數組中出現次數超過一半的數字 java

 一、hashmap:key放array[i],value放他出現的次數,大於數組長度的一半時,返回。 import java.util.HashMap; public class Solution { public int M

原创 [劍指offer] 把數組排成最小的數 java

public String PrintMinNumber(int [] numbers) { String str="";//初始化str for(int i=0;i<numbers.length;i++

原创 [劍指offer] 最小的k個數 java

 一、堆排序 public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { A

原创 [劍指offer] 刪除鏈表中重複的節點 java

public class Solution { public ListNode deleteDuplication(ListNode pHead){ if(pHead==null||pHead.next==nul

原创 [劍指offer] 二叉搜索樹的後序遍歷序列 java

public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { if(sequence.length==0) return

原创 [劍指offer] 從上往下打印二叉樹 java

 用隊列實現。 import java.util.ArrayList; import java.util.Queue; import java.util.LinkedList; public class Solution {

原创 [劍指offer] 包含min函數的棧 java

public class Solution { Stack<Integer> stack=new Stack<Integer>();//存放每次新來的元素 Stack<Integer> minStack=new Stac

原创 [劍指offer] 棧的壓入、彈出序列 java

import java.util.ArrayList; import java.util.Stack; public class Solution { public boolean IsPopOrder(int [] pushA

原创 [劍指offer] 二叉樹的鏡像 java 迭代與遞歸

一、遞歸 public class Solution { public void Mirror(TreeNode root) { if(root!=null){ TreeNode temp

原创 [劍指offer] 順時針打印矩陣 java

public class Solution { public ArrayList<Integer> printMatrix(int [][] matrix) { ArrayList<Integer> list=ne

原创 [劍指offer] 樹的子結構 java

 用兩個遞歸。 第一個用來找A樹中有沒有和B根節點相同的節點, 如果有,進入第二個遞歸,也就是讓A、B樹繼續向下遍歷做判斷; 如果沒有,那就說明B不是A的子樹。 public class Solution { public bo

原创 [劍指offer] 合併兩個排序的鏈表 java

public class Solution { public ListNode Merge(ListNode list1,ListNode list2) { if(list1==null&&list2==null

原创 [劍指offer] 反轉鏈表 java 兩種方法

https://blog.csdn.net/FX677588/article/details/72357389 上面這篇博客的圖解很清晰,看不懂代碼可以看這裏  第一種方法:迭代 head表示當前節點,pre是head的前一個節點,nex

原创 [劍指offer] 求1+2+3+...+n java

 題目限制很多,最開始想的是等差數列求和,但是後來一想,不對。題目不讓用乘除,那就能想到要用邏輯運算了。 public class Solution { public int Sum_Solution(int n) {

原创 [劍指offer] 調整數組順序使奇數位於偶數前面 java(三種方法)

 第一種:又新建了兩個數組,一個放奇數,一個放偶數 public class Solution { public void reOrderArray(int [] array) { int[] array1=ne