剑指offer题目及java实现(1)

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

public class Solution {
    public boolean Find(int target, int [][] array) {
        int l = array.length-1;
        int i = 0;
        while((l >= 0)&& (i < array[0].length)){
            if(array[l][i] > target){
                l--;
            }else if(array[l][i] < target){
                i++;
            }else{
                return true;
            }
        }
        return false;
    }
}

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

public class Solution {
    public String replaceSpace(StringBuffer str) {
    		StringBuffer s1=new StringBuffer();
	char[] s=str.toString().toCharArray();
	for(int i=0;i<s.length;i++)
		if(s[i]!=' '){
			s1=s1.append(s[i]);
		}else if(s[i]==' '){
			s1=s1.append("%20");
		}
	return s1.toString();
    }
}

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

public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    ArrayList<Integer> a=new ArrayList<>();
    	Stack <Integer>st=new Stack<>();
    	while(listNode!=null){
    		st.push(listNode.val);
    		listNode=listNode.next;
    	}
    	while(!st.isEmpty()){
    		a.add(st.pop());
    		
    	}
		return a;
}
}

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

	    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
	  TreeNode root=rTree(pre,0,pre.length-1,in,0,in.length-1);
	        return root;
	    }
	    private TreeNode rTree(int [] pre,int star,int end,int [] in,int startIn,int endIn) {
	          
	        if(star>end||startIn>endIn)
	            return null;
	        TreeNode root=new TreeNode(pre[star]);       
	        for(int i=startIn;i<=endIn;i++)
	            if(in[i]==pre[star]){
	                root.left=rTree(pre,star+1,star+i-startIn,in,startIn,i-1);
	                root.right=rTree(pre,i-startIn+star+1,end,in,i+1,endIn);
	            }             
	        return root; 
	    }
	}
}

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

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.isEmpty())
		{
			while(!stack1.isEmpty())
			{
				int node=stack1.pop();
				stack2.add(node);
			}
		}
		return stack2.pop();
	}
}

6、把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{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=0;
        int max=array.length-1;
        int mid=min;
        while(array[min]>=array[max])
            {
            if(max-min==1){
             mid=max;
                break;
            }
            mid=(min+max)/2;
            if(array[mid]>=array[min]){
                min=mid;
            }
            else if(array[mid]<=array[max]){
                max=mid;
            }  
        }
		    return array[mid]; 
    }
}

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

public class Solution {
    public int Fibonacci(int n) {
		if(n<=0){
            return 0;
        }
       else if(n==1){
            return 1;
                }  
        int a=0;
        int b=1;
        int c=0;
        for(int i=2;i<=n;i++){
            c=a+b;
            a=b;
            b=c;
        }
         return    c;
    }
}

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

public class Solution {
    public int JumpFloor(int target) {
		if(target<=0)
            return 0;
        if(target==1)
            return 1;
        if(target==2)
            return 2;
        int a=1;
        int b=2;
        int c=0;
        for(int j=3;j<=target;j++){
            c=a+b;
            a=b;
            b=c;
        }
        return c;
    }
}

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

public class Solution {
    public int JumpFloorII(int target) {
        int c=1;
        if(target==0)
        {return 0;}
        else if(target==1)
            return 1;
        else for(int i=2;i<=target;i++){
            c=2*c;
        }
        return c;
    }
}

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

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


判断一个数是不是2的整数次方,如果是那他二进制中有且仅有一位是1,这个数减去1在和自己做与运算唯一的一位1也变成了0.

两个整数m和n,计算需要改变m的二进制表示中多少位才能得到n:第一步求两个数异或,第二步求解结果中1的位数。

11、给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

  

public class Solution {
    public double Power(double base, int exponent) {
       if(exponent==0)
        return 1;
    double rest=1.0d;
        for(int i=0;i<Math.abs(exponent);i++){
            rest=rest*base;
        }
        if(exponent>0)
        	return rest;
        else return 1/rest;
}
}

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

public class Solution {
    public void reOrderArray(int [] array) {
      for(int i=0;i<array.length-1;i++){
          for(int j=0;j<array.length-1-i;j++){
              if(array[j]%2==0&&array[j+1]%2==1){
                  int t=array[j];
                  array[j]=array[j+1];
                  array[j+1]=t;
              }
          }
      }
    }
}

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

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

14、输入一个链表,反转链表后,输出链表的所有元素。

public class Solution {
    public ListNode ReverseList(ListNode head) {
if(head==null)
    return null;
        ListNode end =null,p,q;
        p=head; 
        while(p!=null){ 
            q=p.next; 
            p.next=end; 
            end=p; 
            p=q; 
        } 
        return head;
    }
}

15、输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        	if(list1==null){
	    		return list2;
	    	}
	    	else if(list2==null){
	    		return list1;
	    	}
	    	ListNode p=null;
	    		if(list1.val<list2.val)
	    		{
	    			p=list1;
                    p.next=Merge(list1.next,list2);
	    			
	    		}
        else{
                    p=list2;
                    p.next=Merge(list1,list2.next);
                }
	    		return p;	    		
	    	}        
}

16、输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root2==null)
            return false;
       else if(root1==null&root2!=null)
           return false;
      return issun(root1,root2)||HasSubtree(root1.left,root2)|| HasSubtree(root1.right,root2);
}
    public boolean issun(TreeNode root1,TreeNode root2){
if(root2==null)
    return true;
        if(root1==null){
            return false;
        }
        if(root1.val!=root2.val)
            return false;
        return issun(root1.left,root2.left)&&issun(root1.right, root2.right);
	    }
 
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值