### 剑指offer文档版 评分:

testarray[3[0]=6 testarray 3[1]=8 testarray [ 3[2]=11 testarray 33=157 System. out. println(find (testarray 1)) 题面试题4替换空格 题目:请实现一个函数,把字符串中的每个空格替换成"%20″。 public class Replace blank t public static void main( string argsi String s="We are happy. "i System. out. printIn (replaceBlank(s)) public String replaceBlank(string input i if(input==null) return null: String Buffer outputBuffer=new String Buffer for (int i=0; i<inputlength o; i++i if( input charAt(==)i output Buffer append( %" output Buffer append( 2); output Buffer append(0"); 3 else i output Buffer append(string valueof(input charAt()) return new String(outputBuffer) 题面试题5:从尾到头打印链表 题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。 方法一:非递归的方式 public class PrintlistReverse i public static void main(String argsDt ListNode nodel=new ListOoder ListNode node2=new ListOoder ListNode node3=new listNode node,data=1 node2. data=2 node3data =3; node1 next=node2 node2. next=node3 printlistReversversingly test=new printListReversversinglyo: test. printlistReverse(node 1) public static void printlistReverse(listNode headnodei Stack< ListNode> stack=new Stack<ListNode>o: while(headNodel=null& stack push (headNode head Node =head node next: while(Stack is Emptyof System. out. println(stack. popo datai 方法二: 递归方式实现 public class printlistReverse i public static void main (String angst ListNode node1=new listnode ListNode node2=new ListNode o: ListNode node3=new ListNode nodel data=1: node2, data =2; node3 data=3: node1 next=node 2 node2. next=node3; printListReversversingly test=new printListReversversingly o: test. printlistReverse(node 1) public static void printlistReverse(ListNode headNodei if(head Node!=nulli if(headNode. next!=null)t printlistReverse(head Node next) System. out. printIn(headNode data); 题面试题6:重建二叉树 题目描述:输入二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设前 序遍历和中序遍历结果中都不包含重复的数字,例如输入的前序遍历序列 1,2,4,7,3,5,6,8}和中序遍历序列{47,2,1,53,8,6}重建出如图所示的二叉 树。 public class Binary treeNode i public static int value public Binary TreeNode leftNodei public Binary TreeNode rightNode import java. util. Arrays; public class Problem6 t public static void main (String[] args) throws Exception i int[] presort=(1, 2,4, 3, 5, 6, 8; int[] inSort={4,7,21,53,8,6} Binary treeNode root=construct core(presort in Sort): public static Binary TreeNode constructcore(int[] preorder, int[] inorder throws Exception i if(preorder==null inorder==nut eturn nu if(preorder. length! =inorder lengths throw new Exception("长度不一样,非法的输入"); Binary treeNode root=new Binary treenode for(int i=0; i<inorder. length; i++)t if(inorder[==preorder[oDt root value=inorder[li System. out println(root. value) root. leftNode=constructCore(Arrays. copyOf Range(preorder, 1, 1), Arrays. copyofRange(inorder, 0, D); root rightNode=constructCore (Arrays. copy ofRange(preorder, i+ preorder length), Arrays. copy ofRange(inorder, i+1 inorder length)); return root 题面试题7:用两个栈实现队列 题目描述:用两个栈实现一个队列,实现对了的两个函数 appends和 deletehead,分别完成在队列尾插入结点和在队列头部删除结点的功能。 public class problem7<T>t private Stack<T> stack=new Stack<T>o private Stack<T> stack2=new Stack<T>O: public void appendTail(t tt stack 1. push(t: public t deleteHeado throws Exception t if(stack2 isEmpty ot while(stack isEmpty i stack2 push(stack1 popO; if(stack2 isEmpty oi throw new Exception("队列为空,不能删除"); return stack2 popo public static void main(String args] throws Exception Problem 7<String> p7=new Problem7<>0 p7. appendTail( 1 i p7.appendtail(2)i p7.appendtail( 3 i p7. deleteHeadO: 题面试题8:旋转数组的最小数字 题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的 旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数 组3,451,2}为{1,2,34,5}的一个旋转,该数组的最小值为1. public class problem8 t public static void main( String] argsi Problem 8 p8=new Problem80 / int[] array={1,1,1,2,0}; //int[]aray={345,1,2}; nt]aray={1,0,1,1,1}; System. out printIn(p8 findMin Num(array)) public Integer findMinNum(int[] array if(array==null)t return null It leftIndex=0 int rightindex=array length -1 int mid=0: while(array lleftIndex]>=array[rightIndex]i if(rightIndex-leftIndex<=1i mid=rightindex break mid=(leftIndex+rightIndex)/2; if(array lleftindex]==array lrightindex ]&&array lleftIndex]==a rraylmiddi if (arraylleftindex+1!=arraylrightIndex-1]t mid=array lleftIndex+1]<arraylrightIndex-1]? (leftindex+1: r ghtIndex-1 break y else t leftIndex++i rightIndex-- 3 else i if(array[mid]>=arraylleftIndex] leftIndex=mid; else t if(arraylmid]<=array lrightindexd ightindex=mid; return array [mid]; 题面试题9:斐波那契数列 题目一:写一个函数,输入n,求斐波那契数列的第n项。 public class Fibonacci t public long fibonacci(int nt long result =0; long preOne=0; long preTo=1; f(n==0){ return preOne f(n==1){ return preTo for(int i=2 i<=n; i++i result=preOne+preTwo preone=preTo preTo=result, return result 题面试题10:二进制中1的个数 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数。例如 把9表示成二进制是1001;有2位是1,因此如果输入9,函数输出2 public class Problem10 t public static void main(String argsi Problem 10 test=new Problem100 System. out. printin(test numberof 3)) public int numberof(int n)t It count=0 while(n=0. count++i n=(n-1)&n; return count

...展开详情
2018-09-24 上传 大小：412KB

《剑指offer》第二版 高清PDF+代码附赠 64M PDF文档 立即下载