当前位置: 代码迷 >> 综合 >> 剑指Offer算法
  详细解决方案

剑指Offer算法

热度:88   发布时间:2023-10-29 02:40:45.0

1、在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

 public static boolean Find(int target, int [][] array) {int rows=array.length;int columns=array[0].length;int row=0;int column=columns-1;boolean flag=false;//从右上角开始寻找目标值,如果目标值大于数组中对应位置的值,row+1;如果目标值小于数组中对应的值,column-1;while(row<rows&&column>=0){if(array[row][column]==target){flag=true;break;}else if(target<array[row][column])column--;elserow++;}return flag;}

2、输入一个链表,从尾到头打印链表每个节点的值。

public class Solution {//这道题可以先正向的将链表中的数据读出来,然后放到栈中,根据栈的特点,就能将每个节点的值从尾打印到头部public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {if(listNode==null){return null;}ArrayList<Integer> a=new ArrayList<Integer>();Stack<Integer> s=new Stack<Integer>();ListNode temp=listNode;while(temp!=null){s.push(temp.val);temp=temp.next;}while(!s.empty()){a.add(s.pop());}return a;}
}

3、用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型

//此题主要考察栈和队列的性质。栈先入后出,队列先入先出。我们可以先通过stack1入栈,通过stack2出栈,这样就能当做队列了,如果stack2栈中没有数据时,将stack1中数据压入到stack2中
public class Solution {Stack<Integer> stack1 = new Stack<Integer>();Stack<Integer> stack2 = new Stack<Integer>();public void push(int node) {stack1.push(node);}public int pop() {if(stack2.empty()){while(!stack1.empty()){stack2.push(stack1.pop());}}return stack2.pop().intValue();}
}
4、 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
public class Solution {//这道题也就是找出最小值public int minNumberInRotateArray(int [] array) {if(array.length==0)return 0;int min=array[0];for(int i=0;i<array.length;++i){if(min>array[i])min=array[i];}return min;}
}
5、 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

n<=39

  public int Fibonacci(int n) {//f(0)=0,f(1)=1,f(2)=1,f(n)=f(n-1)+f(n-2)if(n==1)return 1;if(n==0)return 0;else{return Fibonacci(n-1)+Fibonacci(n-2);}}

6、一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

  public int JumpFloor(int target) {//对于青蛙来说,跳n级台阶,第一跳,分为两种情况:跳1级台阶;跳2级台阶,这样就有f(n)=f(n-1)+f(n-2);if(target==1||target==2)return target;return JumpFloor(target-1)+JumpFloor(target-2);}

7、一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

public int JumpFloorII(int target) {//f(n)=f(n-1)(相当于第一次跳了1级台阶)+f(n-2)(相当于第一次跳了2级台阶)+f(n-3)+....f(n-(n-1))+f(n-n)=f(0)+f(1)+..f(n-1)=2*f(n-1)if(target==1)return 1;if(target==2)return 2;return 2*JumpFloorII(target-1);}
8、 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
 public int RectCover(int target) {//其实这道题和青蛙跳台阶一样。因为大矩形2*n,横着放只能放一个,竖着放能放两个,对于要填满矩形来说,要么先横着放,则还有f(n-1),要么竖着放,则还有f(n-2)if(target<=2)return target;return RectCover(target-1)+RectCover(target-2);}
9、 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
public class Solution {//这道题的解法:将奇数放到数组前面,将偶数放到数组后面,可以采用冒泡的形式,如果两个相邻的数,左边是偶数,右边是奇数,则调换位置;//如果左边是奇数,右边是偶数,不用调换位置public void reOrderArray(int [] array) {for(int i=0;i<array.length-1;++i){//外层控制遍数for(int j=array.length-1;j>i;j--){//内层进行比较,调换if(array[j]%2!=0&&array[j-1]%2==0){int temp=array[j];array[j]=array[j-1];array[j-1]=temp;}}}}
}

10、输入一个链表,输出该链表中倒数第k个结点

public class Solution {public ListNode FindKthToTail(ListNode head,int k) {if(head==null){  return null;  }  int count=1;  ListNode old=head;  while(head.next!=null){  head=head.next;  count++;  }  if(k>count){  return null;  }  for(int i=0;i<count-k;i++){  old=old.next;  }  return old;  }
}




  相关解决方案