java結構中:
Queue:offer,poll,isEmpty,peek
定義隊列:Queue<Integer> queue = new LinkedList<Integer>();
Stack:push,pop,Empty,peek
一 二維數組中查找:
在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
1.int[][] array, target,row = (二維數組的行數) col = (二維數組的列數)
2.從左下角或右上角開始掃描。
3.當target == 值時返回,當target<值時row++,當target>值時col--;
4.row界爲row<array.length, col >= 0;
二 :替換空格:
請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。
1. 若改字符串是StringBuffer長度可變類型的,則可新建一個StringBuffer從前往後遍歷,遇到' '時就改變。
2.若是不可變類型的,則先遍歷一遍字符串,查看空格個數n,最後新建一個長度爲2*n+str.length長度的數組存放新的數。
三:重建二叉樹:
輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
1.用遞歸的方法:pre[0],用來建一個根節點root,查找在in[]數組中與pre[0]相同的index記爲i。
2.root.left = construct(Arrays.copyOfRange(pre,1,i+1), Arrays.copyOfRange(in, 0, i))
3.root.right = construct(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1, in.length))
5. return root
四:用兩個棧來實現隊列:
1.stack1 和stack2同時爲空時隊列爲空。
2.stack1不空,stack2空時,把stack1的元素先搬到Stack2中。
3.進隊列時,直接往stack1進就好。
五:二進制中1的個數:
輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。
1.public int NumberOf1(int n) {
int count = 0;
while(n != 0){
n = n & (n-1);
count++;
}
return count;
}
一個數減1後,最右邊的那個1後邊(包括自己)所有的0會變爲1.
六:數值的整數次方
給定一個double類型的浮點數base和int類型的整數exponent。求base的exponent次方。
class Solution {
public:
double Power(double base, int exponent) {
long long p = abs((long long)exponent);
double r = 1.0;
while(p){
if(p & 1) r *= base;
base *= base;
p >>= 1;
}
return exponent < 0 ? 1/ r : r;
}
};
七:遞歸法翻轉鏈表
1.
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode reverse = ReverseList(head.next);
head.next.next = head;
head.next = null;
return reverse;
}
}
八:樹的子結構
輸入兩棵二叉樹A,B,判斷B是不是A的子結構。(ps:我們約定空樹不是任意一個樹的子結構)
1.用一個函數isSubTree判斷以此root1, root2爲根的樹是否相同。
2.用一個函數HasSubTree尋找他們具有相同的根時進入isSubTree.
九:順時針打印矩陣
1.用一個標記start記錄打印的圈數.endrow = row - start -1; endcol = col - start - 1;
2.要考慮一個數,一行,一列的(兩行,兩列)的時候。
十:棧的壓入和彈出順序
1.pushA:入棧順序,popA:出戰順序,push入棧指針,pop出棧指針
2.用push == pushA.length 判斷是否入棧完畢
3.while循環出完相同的元素,用stack.peek前要判斷stack.empty。否則空棧時用stack.peek會出錯。
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
boolean isPossible = false;
if(pushA.length == 0 || popA.length == 0)
return isPossible;
Stack<Integer> stack = new Stack<Integer>();
int push = 0;
int pop = 0;
while(push != pushA.length){
stack.push(pushA[push]);
while(!stack.empty() && stack.peek() == popA[pop]){
pop++;
stack.pop();
}
push++;
}
if(stack.empty())
return true;
else
return false;
}
}
十:判斷二叉樹是否爲後續遍歷序列
1.sequence爲要判斷的序列。
2.取序列最後的一個數爲root.
3.把序列從前往後遍歷小於root爲左,直到找到大於root的爲第一個右子樹的節點,將右子樹節點與root比較。
4.若在右子樹中還找到數字小於root,則判斷這顆樹爲false,若start >= end時 證明判斷到結束,這棵樹爲後續遍歷二叉搜索樹。
5.將左右子樹分別遍歷。
十一:二叉樹中和爲某一值的路徑
import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
int countSum = 0;
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
if(root == null)
return list;
ArrayList<Integer> al = new ArrayList<Integer>();
findPath(root, target, countSum, al, list);
return list;
}
public void findPath(TreeNode root, int target, int countSum, ArrayList<Integer> al, ArrayList<ArrayList<Integer>> list){
if(root == null)
return;
countSum += root.val;
if(root.left == null && root.right == null){
if(target == countSum){
al.add(root.val);
list.add(new ArrayList<Integer>(al));
al.remove(al.size()-1);
}
return;
}
al.add(root.val);
findPath(root.left, target, countSum, al, list);
findPath(root.right, target, countSum, al, list);
al.remove(al.size()-1);
}
}
十二:複雜鏈表的複製:
輸入一個複雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點),返回結果爲複製後複雜鏈表的head。(注意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)
1.先將所有節點在原鏈表後面複製一個。
2.將random的指針根據上一個節點定義。
3.將鏈表斷開。將複製的鏈表抽出來。