劍指offer演算法練習1-10
/**
* 1:題目描述:
* 在一個二維陣列中(每個一維陣列的長度相同),
* 每一行都按照從左到右遞增的順序排序,
* 每一列都按照從上到下遞增的順序排序。
* 請完成一個函式,輸入這樣的一個二維陣列和一個整數,
* 判斷陣列中是否含有該整數。
*/
public class Main1 {
/* public boolean main1(int target, int [][] array) {
for(int i=0; i<array.length; i++){
for(int j=0; j<array[0].length; j++){
if (target==array[i][j]){
return true;
}else if(target<array[i][j]){
continue;
}
}
}
return false;
}*/
public boolean Main1(int target, int [][] array) {
int row=array.length-1;
int col=0;
while(row>-1&&col<array[0].length){
if(target==array[row][col]){
return true;
}else if(target>array[row][col]){
col++;
}else{
row--;
}
}
return false;
}
}
/**
* 2:題目描述
* 請實現一個函式,將一個字串中的每個空格替換成“%20”。
* 例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。
*/
public class Main2 {
public String replaceSpace(StringBuffer str) {
String st = new String();
char[] c = str.toString().toCharArray();
for(int i=0; i<c.length; i++){
if(String.valueOf(c[i]).equals(" ")){
st+="%20";
continue;
}
st+=c[i];
}
return st;
}
}
/**
* 3:題目描述
* 輸入一個連結串列,按連結串列值從尾到頭的順序返回一個ArrayList。
* public class Main3ListNode {
* int val;
* Main3ListNode next = null;
*
* Main3ListNode(int val) {
* this.val = val;
* }
* }
*
*/
public class Main3 {
public ArrayList<Integer> printListFromTailToHead(Main3ListNode listNode) {
ArrayList<Integer> arrayList = new ArrayList<>();
Stack <Integer> temp = new Stack<>();
while(listNode!=null){
temp.push(listNode.val);
listNode=listNode.next;
}
while(!temp.empty()){
arrayList.add(temp.pop());
}
return arrayList;
}
}
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
* 4:題目描述
* 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。
* 假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
* 例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。
*/
public class Main4 {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root = reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
return root;
}
//前序遍歷{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBinaryTree(pre,startPre+1,startPre+(i-startIn),in,startIn,i-1);
root.right=reConstructBinaryTree(pre,startPre+(i-startIn)+1,endPre,in,i+1,endIn);
break;
}
return root;
}
}
/**
* 5:題目描述
* 用兩個棧來實現一個佇列,完成佇列的Push和Pop操作。
* 佇列中的元素為int型別。
*/
public class Main5 {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
//入佇列
public void push(int node) {
stack1.push(node);
}
//出佇列
public int pop() {
if(stack1.empty()&&stack2.empty()){
throw new RuntimeException("Queue is empty!");
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
/**
* 6:題目描述
* 把一個數組最開始的若干個元素搬到陣列的末尾,我們稱之為陣列的旋轉。
* 輸入一個非減排序的陣列的一個旋轉,輸出旋轉陣列的最小元素。
* 例如陣列{3,4,5,1,2}為{1,2,3,4,5}的一個旋轉,該陣列的最小值為1。
* NOTE:給出的所有元素都大於0,若陣列大小為0,請返回0。
*/
public class Main6 {
public int minNumberInRotateArray(int [] array) {
if(array.length==0){
return 0;
}
int i;
for(i=0; i+1<array.length; i++){
if(array[i]>array[i+1]){
break;
}
}
return array[i+1];
}
}
/**
* 7:題目描述
* 大家都知道斐波那契數列,現在要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項為0)。
* n<=39
*/
public class Main7 {
public int Fibonacci(int n) {
if(n<=0){
return 0;
}else if(n==1||n==2){
return 1;
}
int a=1,b=1,c=0;
for(int i=3; i<=n; i++){
c=a+b;
a=b;
b=c;
}
return c;
}
}
/**
* 8:題目描述
* 一隻青蛙一次可以跳上1級臺階,也可以跳上2級。
* 求該青蛙跳上一個n級的臺階總共有多少種跳法(先後次序不同算不同的結果)
* 找規律:歸納法
*/
public class Main8 {
public int JumpFloor(int target) {
if (target <= 0) {
return -1;
} else if (target == 1) {
return 1;
} else if (target ==2) {
return 2;
} else {
return JumpFloor(target-1)+JumpFloor(target-2);
}
}
}
/**
* 9:題目描述
* 一隻青蛙一次可以跳上1級臺階,也可以跳上2級……它也可以跳上n級。
* 求該青蛙跳上一個n級的臺階總共有多少種跳法。
*/
public class Main9 {
public int JumpFloorII(int target) {
if (target == 1) {
return 1;
} else {
return 2 * JumpFloorII(target - 1);
}
}
}
/**
* 10:題目描述
* 我們可以用2*1的小矩形橫著或者豎著去覆蓋更大的矩形。
* 請問用n個2*1的小矩形無重疊地覆蓋一個2*n的大矩形,總共有多少種方法?
*/
public class Main10 {
public int RectCover(int target) {
if(target<=0){
return 0;
}else if(target==1||target==2){
return target;
}else{
return RectCover(target-1)+RectCover(target-2);
}
}
}