剑指offer例题(Java编程通过).docx_第1页
剑指offer例题(Java编程通过).docx_第2页
剑指offer例题(Java编程通过).docx_第3页
剑指offer例题(Java编程通过).docx_第4页
剑指offer例题(Java编程通过).docx_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面试题3:二维数组中的查找P38public class Solution public boolean Find(int array,int target) int m = 0;/行 int i = array.length-1;/列 while(m =0) if(arraymi target)/与左上的元素相比较 i-; else if(arraymi target) m+; else return true; return false; 面试题4:替换空格P44public class Solution public String replaceSpace(StringBuffer str) char a=new charstr.length(); for(int i=0;istr.length();i+) ai=str.charAt(i); StringBuffer ss = new StringBuffer(); for(int i=0;ia.length;i+) if(ai= ) ss.append(%20); else ss.append(ai); String s = ss.toString(); /System.out.println(s); return s; 面试题5:输入一个链表,从尾到头打印链表每个节点的值。P51/* * public class ListNode * int val;* ListNode next = null;* ListNode(int val) * this.val = val;* * */import java.util.ArrayList;import java.util.Stack;public class Solution public ArrayList printListFromTailToHead(ListNode listNode) Stack stack = new Stack(); ArrayList list=new ArrayList();/新生成的从后到前的链 ListNode current=listNode; while(current!=null) stack.push(current); current=current.next; while(!stack.isEmpty() list.add(new Integer(stack.pop().val); return list; 面试题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) /pre前序 in中序 TreeNode root=reConstruct(pre,0,pre.length-1,in,0,in.length-1); return root; /前序遍历1,2,4,7,3,5,6,8和中序遍历序列4,7,2,1,5,3,8,6 private TreeNode reConstruct(int pre,int startPre,int endPre,int in,int startIn,int endIn) if(startPreendPre|startInendIn) return null; TreeNode root=new TreeNode(prestartPre); for(int i=startIn;i=endIn;i+) if(ini=prestartPre) root.left=reConstruct(pre,startPre+1,startPre+i-startIn,in,startIn,i-1); root.right=reConstruct(pre,i-startIn+startPre+1,endPre,in,i+1,endIn); return root; 面试题7:用两个栈实现队列P59import java.util.Stack;public class Solution Stack stack1 = new Stack(); Stack stack2 = new Stack(); public void push(int node) stack1.push(new Integer(node); public int pop() while(!stack2.isEmpty() return stack2.pop(); while(!stack1.isEmpty() stack2.push(stack1.pop(); return stack2.pop(); 面试题8:旋转数组的最小数字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 mid = left; if(arrayleft=arrayright) if(right-left =1) mid=left; break; mid=(left+right)/2; if(arraymid=arrayleft) left=mid; else if (arraymid=arrayleft) right=mid; return arraymid; 面试题9:斐波那契数列public class Solution public int Fibonacci(int n) /非递归解法 int array=0,1; if (n=0) return 0; if (n2) return arrayn; int left = 0; int right = 1; int fib = 0; for(int i=1;in;i+) fib = left+right; left=right; right = fib; return fib; 面试题10:二进制中1的个数面试题11:数值的整数次方public class Solution public double Power(double base , int exp) int absexp=0; if(base=0.0&exp0) return 0.0;/底数为零指数小于零的情况 if (exp0) absexp=(-1)*exp; else absexp=exp; double result = Pow(base,absexp); if(exp0) result = 1.0/result; return result; double Pow(double base,int absexp) double re = 1.0; for(int i=1;i=absexp;i+) re *= base; return re; 面试题12:打印1到最大的n位数面试题14:调整数组顺序使奇数位于偶数前面/运行时间超时public void reOrderArray(int array) int i = 0; int temp = 0; while(iarray.length) if(arrayi%2=0) temp = arrayi; arrayi=arrayi+1; arrayarray.length-1=temp; i-; i+; 面试题15:链表中倒数第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|k=0)/鲁棒性 return null; ListNode pre=head; ListNode last=head; for(int i=1;ik;i+) if(pre.next!=null)/鲁棒性 pre=pre.next; else return null; while(pre.next!=null) pre = pre.next; last = last.next; return last; 面试题16:反转链表/*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) return null; if (head.next = null) return head; ListNode pPre = null; ListNode p = head; ListNode pNext = head.next; ListNode newHead = null; while (p != null) pNext = p.next;/一定要记录下来后面的节点 if (pNext = null) newHead = p; p.next = pPre;/这里的方向已经转变 pPre = p;/向后走了一位 p = pNext; return newHead; 面试题17:合并两个排序列表/*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&list2=null) return null; else if (list1=null) return list2; else if (list2=null) return list1; /前四行考虑鲁棒性 ListNode newHead = null; ListNode p = list1; ListNode q = list2; ListNode temp = newHead; while(p!=null&q!=null) if(p.val=q.val) if(newHead=null) newHead=temp=p; else temp.next=p; temp=temp.next; p=p.next; else if(newHead=null) newHead=temp=q; else temp.next=q; temp=temp.next; q=q.next; if(p=null) temp.next=q; if(q=null) temp.next=p; return newHead; 面试题18:树的子结构/*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) if(root2=null) return false; if(root1=null & root2!=null) return false; boolean flag = false; if(root1.val=root2.val) flag = DoesTree1HaveTree2(root1,root2); if(!flag) flag = HasSubtree(root1.left, root2); if(!flag) flag = HasSubtree(root1.right, root2); return flag; boolean DoesTree1HaveTree2(TreeNode p1,TreeNode p2) if(p2=null) return true; if(p1=null) return false; if(p1.val!=p2.val) return false; return DoesTree1HaveTree2(p1.left,p2.left)&DoesTree1HaveTree2(p1.right,p2.right); 面试题19:二叉树的镜像/*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 ; if(root.left=null&root.right=null) return; TreeNode temp = null; temp = root.left; root.left = root.right; root.right = temp; if(root.left!=null) Mirror(root.left); if(root.right!=null) Mirror(root.right); 面试题21:包含min函数的栈import java.util.Stack;public class Solution Stack stack = new Stack(); Stack minStack = new Stack(); int min ; public void push(int node) stack.push(node); if(minStack.isEmpty() minStack.push(node); else min = minStack.peek(); min = node min ? minStack.push(node) : minStack.push(min); public void pop() if(!stack.isEmpty() & !minStack.isEmpty() stack.pop(); minStack.pop(); public int top() return stack.peek();/peek 查看栈顶对象而不移除 public int min() return minStack.peek(); 面试题22:栈的压入弹出序列/*思路:先循环将pushA中的元素入栈,遍历的过程中检索popA可以pop的元素*如果循环结束后栈还不空,则说明该序列不是pop序列。*/import java.util.ArrayList;import java.util.Stack;public class Solution public boolean IsPopOrder(int pushA,intpopA) Stack stack = new Stack(); if( pushA.length= 0 & popA.length= 0 ) return false; for( int i=0,j=0; i pushA.length; i+ ) stack.push( pushAi ); while( ( !stack.empty() )& ( stack.peek() = popAj ) ) stack.pop(); j +; return stack.empty() = true; 面试题23:从上往下打印二叉树import java.util.*;/*java中队列queue的使用:add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常offer 添加一个元素并返回true 如果队列已满,则返回falsepoll 移除并返问队列头部的元素 如果队列为空,则返回nullpeek 返回队列头部的元素 如果队列为空,则返回nullput 添加一个元素 如果队列满,则阻塞take 移除并返回队列头部的元素 如果队列为空,则阻塞*/public class Solution public ArrayList PrintFromTopToBottom(TreeNode root) ArrayList list = new ArrayList(); /list 存放输出序列 if(root=null) return list; Queue queue = new LinkedList(); queue.offer(root); while (!queue.isEmpty() TreeNode treeNode = queue.poll(); if (treeNode.left != null) queue.offer(treeNode.left); if (treeNode.right != null) queue.offer(treeNode.right); list.add(treeNode.val); return list; 面试题24:二叉搜索树后序遍历序列import java.util.*;public class Solution public boolean VerifySquenceOfBST(int sequence) if(sequence =null|sequence.length=0) return false; int root = sequencesequence.length-1; int i =0; for(i=0;iroot) break; for(int j=i;jsequence.length-1;j+) if(sequencej0) left=VerifySquenceOfBST(Arrays.copyOfRange(sequence, 0, i); if(isequence.length-1) right=VerifySquenceOfBST(Arrays.copyOfRange(sequence, i, sequence.length-1); return (left&right); /* System.arraycopy(src, 2, dest, 5, 1);/从src中的第2个位置到dest的第5个位置;总数为1个dest=Arrays.copyOfRange(src, 2, 4);/从src中的第2个位置到第4个位置;总数为2个 */ 面试题25:二叉树中和为某一值的路径import java.util.*;/* 算法思路:(1)采用深度优先算法,利用栈结构存储访问过的路径结点的值。 (2)当访问到空节点时,直接return 。 (3)当访问到叶子结点时,判断走过的路径的值之和是否为target,如果是,则保存此路径,否则不处理。 (4)先访问左子树,后访问右子树。 (5)在访问完某个结点的左右子树时,需要将此结点的值出栈。*/public class Solution public ArrayListArrayList FindPath(TreeNode root,int target) ArrayListArrayList lis = new ArrayListArrayList(); Stack stack = new Stack(); dfs(lis,root,stack,target); return lis; /深度优先 public void dfs (ArrayListArrayList list, TreeNode root, Stack stack, int target) if(root = null) return ; if(root.left = null & root.right = null) /如果是叶子节点 if(target = root.val) Iterator iter = stack.iterator(); /栈的迭代器 ArrayList tmp = new ArrayList(); while(iter.hasNext() tmp.add(iter.next(); /如果满足条件就将这条路径保存下来 tmp.add(root.val); list.add(tmp); return ; /不是叶子节点就入栈 stack.push(root.val); /递归左右子树 dfs(list,root.left,stack,target-root.val); dfs(list,root.right,stack,target-root.val); /返回到该节点的父节点处 stack.pop(); 面试题26:复杂链表的复制(分步法)import java.util.*;/*public class RandomListNode 分步解决法:int label;RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) this.label = label; 1.根据原始链表的每个节点N创建对应的N,我们将N链在N的后面。2.设置复制出来的random,N是N的next指向的节点,那么S也是S的next指向的结点3.拆分这个链表:奇数位置是原有的链表,偶数位置是复制出来的链表。*/public class Solution public RandomListNode Clone(RandomListNode pHead) CloneNodes(pHead); ConnectRandom(pHead); return (Reconnect(pHead); void CloneNodes(RandomListNode pHead) RandomListNode pNode = pHead; while(pNode != null) RandomListNode pCloned = new RandomListNode(0); pCloned.next = pNode.next; pCloned.label = pNode.label; pCloned.random = null; pNode.next = pCloned; pNode = pCloned.next; void ConnectRandom(RandomListNode pHead) RandomListNode pNode = pHead; while(pNode!=null) RandomListNode pCloned = pNode.next; if(pNode.random!=null) pCloned.random = pNode.random.next; /是原始random节点的下一个节点 pNode = pCloned.next; RandomListNode Reconnect(RandomListNode pHead) RandomListNode pNode = pHead; RandomListNode pClonedHead = null; RandomListNode pClonedNode = null; if(pNode != null)/处理头节点 pClonedHead = pClonedNode = pNode.next; pNode.next = pClonedNode.next; pNode = pNode.next; /pNode向后走一位 while(pNode != null) pClonedNode.next = pNode.next; pClonedNode = pClonedNode.next; pNode.next = pClonedNode.next; pNode = pNode.next; return pClonedHead; 面试题27:二叉搜索树与双向链表非递归:/*public class TreeNode int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) this.val = val; 方法一:非递归版解题思

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论