剑指offer例题Java编程通过.docx

上传人:b****2 文档编号:12880266 上传时间:2023-04-22 格式:DOCX 页数:56 大小:26.70KB
下载 相关 举报
剑指offer例题Java编程通过.docx_第1页
第1页 / 共56页
剑指offer例题Java编程通过.docx_第2页
第2页 / 共56页
剑指offer例题Java编程通过.docx_第3页
第3页 / 共56页
剑指offer例题Java编程通过.docx_第4页
第4页 / 共56页
剑指offer例题Java编程通过.docx_第5页
第5页 / 共56页
点击查看更多>>
下载资源
资源描述

剑指offer例题Java编程通过.docx

《剑指offer例题Java编程通过.docx》由会员分享,可在线阅读,更多相关《剑指offer例题Java编程通过.docx(56页珍藏版)》请在冰豆网上搜索。

剑指offer例题Java编程通过.docx

剑指offer例题Java编程通过

面试题3:

二维数组中的查找P38

publicclassSolution{

publicbooleanFind(int[][]array,inttarget){

intm=0;//行

inti=array.length-1;//列

while(m=0){

if(array[m][i]>target)//与左上的元素相比较

i--;

elseif(array[m][i]

m++;

else

returntrue;

}

returnfalse;

}

}

面试题4:

替换空格P44

publicclassSolution{

publicStringreplaceSpace(StringBufferstr){

chara[]=newchar[str.length()];

for(inti=0;i

{

a[i]=str.charAt(i);

}

StringBufferss=newStringBuffer();

for(inti=0;i

{

if(a[i]=='')

{

ss.append("%20");

}

else

{

ss.append(a[i]);

}

}

Strings=ss.toString();

//System.out.println(s);

returns;

}

}

面试题5:

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

P51

/**

*publicclassListNode{

*intval;

*ListNodenext=null;

*

*ListNode(intval){

*this.val=val;

*}

*}

*

*/

importjava.util.ArrayList;

importjava.util.Stack;

publicclassSolution{

publicArrayListprintListFromTailToHead(ListNodelistNode){

Stackstack=newStack();

ArrayListlist=newArrayList();//新生成的从后到前的链

ListNodecurrent=listNode;

while(current!

=null){

stack.push(current);

current=current.next;

}

while(!

stack.isEmpty()){

list.add(newInteger(stack.pop().val));

}

returnlist;

}

}

面试题6:

重建二叉树

/**

*Definitionforbinarytree

*publicclassTreeNode{

*intval;

*TreeNodeleft;

*TreeNoderight;

*TreeNode(intx){val=x;}

*}

*/

publicclassSolution{

publicTreeNodereConstructBinaryTree(int[]pre,int[]in){

//pre前序in中序

TreeNoderoot=reConstruct(pre,0,pre.length-1,in,0,in.length-1);

returnroot;

}

//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}

privateTreeNodereConstruct(int[]pre,intstartPre,intendPre,int[]in,intstartIn,intendIn)

{

if(startPre>endPre||startIn>endIn)

returnnull;

TreeNoderoot=newTreeNode(pre[startPre]);

for(inti=startIn;i<=endIn;i++)

if(in[i]==pre[startPre]){

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);

}

returnroot;

}

}

面试题7:

用两个栈实现队列P59

importjava.util.Stack;

publicclassSolution{

Stackstack1=newStack();

Stackstack2=newStack();

publicvoidpush(intnode){

stack1.push(newInteger(node));

}

publicintpop(){

while(!

stack2.isEmpty())

{

returnstack2.pop();

}

while(!

stack1.isEmpty())

{

stack2.push(stack1.pop());

}

returnstack2.pop();

}

}

面试题8:

旋转数组的最小数字

importjava.util.ArrayList;

publicclassSolution{

publicintminNumberInRotateArray(int[]array){

if(array.length==0)

return0;

intleft=0;

intright=array.length-1;

intmid=left;

if(array[left]

{

returnarray[left];

}

while(array[left]>=array[right])

{

if(right-left==1)

{

mid=left;

break;

}

mid=(left+right)/2;

if(array[mid]>=array[left])

left=mid;

elseif(array[mid]<=array[left])

right=mid;

}

returnarray[mid];

}

}

面试题9:

斐波那契数列

publicclassSolution{

publicintFibonacci(intn)

{

//非递归解法

int[]array={0,1};

if(n<=0)return0;

if(n<2)returnarray[n];

intleft=0;

intright=1;

intfib=0;

for(inti=1;i

{

fib=left+right;

left=right;

right=fib;

}

returnfib;

}

}

面试题10:

二进制中1的个数

面试题11:

数值的整数次方

publicclassSolution{

publicdoublePower(doublebase,intexp)

{

intabsexp=0;

if(base==0.0&&exp<0)

return0.0;//底数为零指数小于零的情况

if(exp<0)absexp=(-1)*exp;

elseabsexp=exp;

doubleresult=Pow(base,absexp);

if(exp<0)result=1.0/result;

returnresult;

}

doublePow(doublebase,intabsexp)

{

doublere=1.0;

for(inti=1;i<=absexp;i++)

{

re*=base;

}

returnre;

}

}

面试题12:

打印1到最大的n位数

面试题14:

调整数组顺序使奇数位于偶数前面

//运行时间超时

publicvoidreOrderArray(int[]array)

{

inti=0;

inttemp=0;

while(i

{

if(array[i]%2==0)

{

temp=array[i];

array[i]=array[i+1];

array[array.length-1]=temp;

i--;

}

i++;

}

}

面试题15:

链表中倒数第k个结点

/*

publicclassListNode{

intval;

ListNodenext=null;

ListNode(intval){

this.val=val;

}

}*/

publicclassSolution{

publicListNodeFindKthToTail(ListNodehead,intk)

{

if(head==null||k<=0)//鲁棒性

{

returnnull;

}

ListNodepre=head;

ListNodelast=head;

for(inti=1;i

{

if(pre.next!

=null)//鲁棒性

{

pre=pre.next;

}else

returnnull;

}

while(pre.next!

=null)

{

pre=pre.next;

last=last.next;

}

returnlast;

}

}

面试题16:

反转链表

/*

publicclassListNode{

intval;

ListNodenext=null;

ListNode(intval){

this.val=val;

}

}*/

publicclassSolution

{

publicListNodeReverseList(ListNodehead)

{

if(head==null)

returnnull;

if(head.next==null)

returnhead;

ListNodepPre=null;

ListNodep=head;

ListNodepNext=head.next;

ListNodenewHead=null;

while(p!

=null){

pNext=p.next;//一定要记录下来后面的节点

if(pNext==null)

newHead=p;

p.next=pPre;//这里的方向已经转变

pPre=p;//向后走了一位

p=pNext;

}

returnnewHead;

}

}

面试题17:

合并两个排序列表

/*

publicclassListNode{

intval;

ListNodenext=null;

ListNode(intval){

this.val=val;

}

}*/

publicclassSolution{

publicListNodeMerge(ListNodelist1,ListNodelist2){

if(list1==null&&list2==null)

returnnull;

elseif(list1==null)returnlist2;

elseif(list2==null)returnlist1;

//前四行考虑鲁棒性

ListNodenewHead=null;

ListNodep=list1;

ListNodeq=list2;

ListNodetemp=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;

}

returnnewHead;

}

}

面试题18:

树的子结构

/**

publicclassTreeNode{

intval=0;

TreeNodeleft=null;

TreeNoderight=null;

publicTreeNode(intval){

this.val=val;

}

}

*/

publicclassSolution

{

publicbooleanHasSubtree(TreeNoderoot1,TreeNoderoot2)

{

if(root2==null)returnfalse;

if(root1==null&&root2!

=null)returnfalse;

booleanflag=false;

if(root1.val==root2.val){

flag=DoesTree1HaveTree2(root1,root2);

}

if(!

flag)

flag=HasSubtree(root1.left,root2);

if(!

flag)

flag=HasSubtree(root1.right,root2);

returnflag;

}

booleanDoesTree1HaveTree2(TreeNodep1,TreeNodep2)

{

if(p2==null)returntrue;

if(p1==null)returnfalse;

if(p1.val!

=p2.val)returnfalse;

returnDoesTree1HaveTree2(p1.left,p2.left)&&DoesTree1HaveTree2(p1.right,p2.right);

}

}

面试题19:

二叉树的镜像

/**

publicclassTreeNode{

intval=0;

TreeNodeleft=null;

TreeNoderight=null;

publicTreeNode(intval){

this.val=val;

}

}

*/

publicclassSolution{

publicvoidMirror(TreeNoderoot)

{

if(root==null)return;

if(root.left==null&&root.right==null)return;

TreeNodetemp=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函数的栈

importjava.util.Stack;

publicclassSolution

{

Stackstack=newStack();

StackminStack=newStack();

intmin;

publicvoidpush(intnode)

{

stack.push(node);

if(minStack.isEmpty())

minStack.push(node);

else

{

min=minStack.peek();

min=node

minStack.push(node):

minStack.push(min);

}

}

publicvoidpop(){

if(!

stack.isEmpty()&&!

minStack.isEmpty()){

stack.pop();

minStack.pop();

}

}

publicinttop(){

returnstack.peek();//peek查看栈顶对象而不移除

}

publicintmin(){

returnminStack.peek();

}

}

面试题22:

栈的压入弹出序列

/*思路:

先循环将pushA中的元素入栈,遍历的过程中检索popA可以pop的元素

**如果循环结束后栈还不空,则说明该序列不是pop序列。

*/

importjava.util.ArrayList;

importjava.util.Stack;

publicclassSolution

{

publicbooleanIsPopOrder(int[]pushA,int[]popA)

{

Stackstack=newStack();

if(pushA.length==0&&popA.length==0)returnfalse;

for(inti=0,j=0;i

stack.push(pushA[i]);

while((!

stack.empty())&&(stack.peek()==popA[j])){

stack.pop();

j++;

}

}

returnstack.empty()==true;

}

}

面试题23:

从上往下打印二叉树

importjava.util.*;

/**

<树的层序遍历>

java中队列queue的使用:

add增加一个元索如果队列已满,则抛出一个IIIegaISlabEepeplian异常

remove移除并返回队列头部的元素如果队列为空,则抛出一个NoSuchElementException异常

element返回队列头部的元素如果队列为空,则抛出一个NoSuchElementException异常

offer添加一个元素并返回true如果队列已满,则返回false

poll移除并返问队列头部的元素如果队列为空,则返回null

peek返回队列头部的元素如果队列为空,则返回null

put添加一个元素如果队列满,则阻塞

take移除并返回队列头部的元素如果队列为空,则阻塞

*/

publicclassSolution

{

publicArrayListPrintFromTopToBottom(TreeNoderoot)

{

ArrayListlist=newArrayList();

//list存放输出序列

if(root==null)

{

returnlist;

}

Queuequeue=newLinkedList();

queue.offer(root);

while(!

queue.isEmpty())

{

TreeNodetreeNode=queue.poll();

if(treeNode.left!

=null){

queue.offer(treeNode.left);

}

if(treeNode.right!

=null){

queue.offer(treeNode.right);

}

list.add(treeNode.val);

}

returnlist;

}

}

面试题24:

二叉搜索树后序遍历序列

importjava.util.*;

publicclassSolution

{

publicbooleanVerifySquenceOfBST(int[]sequence)

{

if(sequence==null||sequence.length==0)returnfalse;

introot=sequence[sequence.length-1];

inti=0;

for(i=0;i

{

if(sequence[i]>root)

break;

}

for(intj=i;j

{

if(sequence[j]

returnfalse;

}

booleanleft=true;

booleanr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1