题一 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public class Solution {
public boolean Find(int target, int [][] array) {
int len = array.length-1;
int column = 0;
while(len >=0 && column< array[0].length){
if( array[len][column] == target)
return true;
if(array[len][column] > target)
len --;
else
column ++;
}
return false;
}
}
题二 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
char[] a = new char[str.length()];
for(int i=0; i<str.length();i++){
a[i] = str.charAt(i);
}
StringBuffer s = new StringBuffer();
for (int i = 0; i<a.length; i++){
if (a[i] == ' ') s.append("%20");
else s.append(a[i]);
}
String result = s.toString();
return result;
}
}
题三 输入一个链表,从尾到头打印链表每个节点的值。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.*;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<ListNode> stack = new Stack<ListNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
ListNode curr = listNode;
while( curr != null){
stack.push(curr);
curr = curr.next;
}
while(!stack.isEmpty()){
result.add(new Integer(stack.pop().val));
}
return result;
}
}
题四 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in){
return reConBTree(pre,0,pre.length-1,in,0,in.length-1);
}
public TreeNode reConBTree(int [] pre,int preleft,int preright,int [] in,int inleft,int inright){
if(preleft > preright || inleft> inright)//当到达边界条件时候返回null
return null; //新建一个TreeNode
TreeNode root = new TreeNode(pre[preleft]); //对中序数组进行输入边界的遍历
for(int i = inleft; i<= inright; i++){
if(pre[preleft] == in[i]){ //重构左子树,注意边界条件
root.left = reConBTree(pre,preleft+1,preleft+i-inleft,in,inleft,i-1); //重构右子树,注意边界条件
root.right = reConBTree(pre,preleft+i+1-inleft,preright,in,i+1,inright);
}
}
return root;
}
}
题五 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
import java.util.Stack;
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(stack1.empty() && stack2.empty()){
throw new RuntimeException("queue is empty");
}
if (stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
题六 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length ==0) return 0;
int left = 0;
int right = array.length -1;
int middle = -1;
while(array[left] >= array[right]){
if(right - left ==1){
middle = right;
break;
}
middle = left +(right - left)/2;
if(array[left] <= array[middle]) left = middle;
if(array[right] >= array[middle]) right = middle;
}
return array[middle];
}
}
题七 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项 n<=39。
public class Solution {
public int Fibonacci(int n) {
if(n==0 || n==1)
return n;
int one = 0;
int two = 1;
int result = 0;
for(int i = 2; i<=n;i++){
result = one + two;
one = two;
two = result;
}
return result;
}
}
题八 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
public class Solution {
public int JumpFloor(int target) {
if(target == 1 || target == 2)
return target;
int one = 1;
int two = 2;
int result = 0;
for(int i=3; i<=target; i++){
result = one + two;
one = two;
two = result;
}
return result;
}
}
题九 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
public class Solution {
public int JumpFloorII(int target) {
if(target <= 0) return 0;
if(target == 1) return target;
return 2* JumpFloorII(target -1);
}
}
题十 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
public class Solution {
public int RectCover(int target) {
if(target == 1 || target == 2) return target;
int one =1;
int two =2;
int result = 0;
for(int i=2; i<target;i++){
result = one + two;
one = two;
two = result;
}
return result;
}
}