剑指offer-java(1)

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

public class Solution {
    public boolean Find(int [][] array,int target) {
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array[0].length;j++){
                if(target==array[i][j])
                    return true;
            }
        }
        return false;
    }
}

(2)替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

public class Solution {
    public String replaceSpace(StringBuffer str) {
        String s=str.toString().replaceAll(" ","\\%20");
        return s;
    }
}

(3)从尾到头打印链表
输入一个链表,从尾到头打印链表每个节点的值。
输入描述:
输入为链表的表头
输出描述:
输出为需要打印的“新链表”的表头

*    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) {
        ArrayList<Integer> list=new ArrayList<Integer>();
        LinkedList<Integer> stack=new LinkedList<Integer>();
        ListNode p=listNode;
        if(listNode==null)
            return list;
        while(p!=null){
            stack.push(p.val);
            p=p.next;
        }
        while(!stack.isEmpty()){
            list.add(stack.pop());
        }
        return list;
    }
}

(4)重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

/**
 1. Definition for binary tree
 2. public class TreeNode {
 3.     int val;
 4.     TreeNode left;
 5.     TreeNode right;
 6.     TreeNode(int x) { val = x; }
 7. }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
    public reConstructBinaryTree(int[] pre,int startp,int endp,int[] in, int startin, int endin ){
     //先序遍历的第一个数为根节点
    //中序遍历的根节点在中间,左边为左子树,右边为右子树
        if(startp>endp||startin>endin)
            return null;
        TreeNode root=new TreeNode(pre[startp]);
        for(int i=0;i<in.length;i++){
            if(in[i]==pre[startp])  {
                root.left=reConstructBinaryTree(pre,startp+1,i-startin+startp,in,startin,i-1);
                root.right=reConstructBinaryTree(pre,i-1-startin+1+startp,end,in,i+1,endin);
            }
        }
        return root;
    }
}

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

import java.util.Stack;
import java.util.*;
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        stack1.push(node);
    }
    //两个栈实现队列,
    //由于栈是“后进先出”则
    //把数据全部进入栈1中,然后逆序输出到栈2中,接着逆序输出栈2中的数,则
    //实现了队列的先进先出

    //对于两个队列实现栈的问题
    //则队列1和队列2来回交换至只剩下一个数,出队列,实现栈的后进先出
    public int pop() {
        int node=0;
        if(!stack2.isEmpty()){
            node=stack2.pop();

        }else{
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            node=stack2.pop();

        }
        return node;

    }
}

(6)旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。

import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        int min=Integer.MAX_VALUE;
        if(array.length==0)
            return 0;
        for(int i=0;i<array.length;i++){
            if(min>=array[i])
                min=array[i];
        }
        return min;
    }
}

(7)斐波那契数列
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

public class Solution {
    public int Fibonacci(int n) {
        if(n==0)return 0;
        int[] f=new int[n+2];
        f[0]=0;f[1]=1;
        for(int i=2;i<f.length;i++){
            f[i]=f[i-1]+f[i-2]; 
        }
        return f[n];
    }
}

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

public class Solution {
    public int JumpFloor(int target) {
        if(target==0)
            return 0;
        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 Solution {
    public int JumpFloorII(int target) {
            return JumpFloor(target);
    }
    public int JumpFloor(int target){
        int sum=1;
        if(target==0)
            return 0;
        if(target==1)
            return 1;
        if(target==2)
            return 2;
        for(int i=0;i<target;i++){
            sum+=JumpFloor(i);
        }
        return sum;
    }
}

(10)矩形覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

public class Solution {
    public int RectCover(int target) {
    if(target==0)
        return 0;
    else if(target==1)
        return 1;
    else if(target==2)
        return 2;
    else
        return RectCover(target-1)+RectCover(target-2);
    }
}

(11)二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

public class Solution {
    public int NumberOf1(int n) {
        int count=0;
        while(n!=0){
            n=n&(n-1);
            count++;
        }
        return count;
    }
}

(12)数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

public class Solution {
    public double Power(double base, int exponent) {
        double a=base;
        if(exponent>0){
            while((exponent--)>1){
                base=base*a;
            }
            return base;
        }else if(exponent<0){
            while((exponent++)<-1){
                base=base*a;
            }
            return 1.0/base;
        }
        else{
            return 1.00000;
        }
  }
}

(13)调整数组顺序使奇数位于偶数前面
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

import java.util.*;
public class Solution {
    public void reOrderArray(int [] array) {
        List<Integer> odd=new ArrayList<Integer>();
        List<Integer> oven=new ArrayList<Integer>();
        for(int i=0;i<array.length;i++){
            if(array[i]%2==1)        
                odd.add(array[i]);
            else
                oven.add(array[i]);
        }
        int m=0;
        for(int i=0;i<odd.size();i++){
            array[m++]=odd.get(i);
        }
        for(int i=0;i<oven.size();i++){
            array[m++]=oven.get(i);
        }
    }
}

(14)链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null)
            return head;
        if(k<=0)
            return null;
        ListNode p1=head;
        ListNode p2=head;
        while(p1!=null&&(--k)>0){
            p1=p1.next;
        }
        if(p1==null)
            return null;
        while(p1.next!=null){
            p1=p1.next;
            p2=p2.next;
        }
        return p2;
    }
}

(15)反转链表
输入一个链表,反转链表后,输出链表的所有元素。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null)
            return head;
        ListNode newHead=null;
        ListNode pre=null;
        ListNode pnext=null;
        ListNode p=head;
        while(p!=null){
            pnext=p.next;
            if(pnext==null) 
                newHead=p;
            p.next=pre;
            pre=p;
            p=pnext;
        }
        return newHead;
    }
}

(16)合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null)
            return list2;
        else if(list2==null)
            return list1;
        ListNode MergeList=null;
        if(list1.val<list2.val){
            MergeList=list1;
            MergeList.next=Merge(list1.next,list2);
        }else{
            MergeList=list2;
            MergeList.next= Merge(list1,list2.next);
        }
        return MergeList;
    }
}

(17)树的子结构
输入两颗二叉树A,B,判断B是不是A的子结构。

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        boolean result=false;
        if(root1==null)return false;
        if(root2==null&&root1!=null)
            return true;
    if(root1!=null&&root2!=null){
        if(root1.val==root.val){
            result=DoesRoot1HasRoot2(root1,root2)   ;
        }
        if(!result){
            result=HasSubtree(root1.left,root2);
        }
        if(!result){
        result=HasSubtree(root1.right,root2);
        }
    }
    return result;
    }
    public boolean DoesRoot1HasRoot2(TreeNode root1,TreeNode root2)  {
        if(root1==null&&root2!=null)
            return false;
        if(root2==null)
            return true;
        if(root1.val!=root2.val)
            return false;
        return DoesRoot1HasRoot2(root1.left,root2.left)&DoesRoot1HasRoot2(root1.right,root2.right);
    }
}

(18)二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root==null)
            return null;
    TreeNode rootLeft=root.left;
    TreeNode rootRight=root.right;
    root.left=rootRight;
    root.right=rootLeft;
    Mirror(root.left);
    Mirror(root.right);
    }
}

(19)顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> list=new ArrayList<Integer>();
       int row=matrix.length;
       int col=matrix[0].length;
       int top=0,bottom=row-1,left=0,right=col-1;
       while(left<=right&&top<=bottom){
           for(int i=left;i<=right;i++){
               list.add(matrix[0][i]);
           }
           for(int i=top+1;i<=bottom;i++){
               list.add(matrix[i][right]);
           }
           if(top!=bottom)
               for(int i=right-1;i>=left;i--){
                   list.add(matrix[bottom][i]);
               }
           if(left!=right)
               for(int i=bottom-1;i>top;i--){
                   list.add(matrix[i][left]);
               }
           top++;left++;right--;bottom--;
       }
       return list;
    }
}

(20)包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

import java.util.Stack;
import java.util.*;
public class Solution {
    LinkedList<Integer> stack=new LinkedList<Integer>();

    public void push(int node) {
        stack.push(node);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        int i=stack.peek();
        return i;
    }

    public int min() {
        int min=stack.peek();
        int tmp=0;
        Iterator<Integer> it=stack.iterator();
        while(it.hasNext()){
            tmp=it.next();
            if(min>tmp)        
                min=tmp;
        }
        return min;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值