算法面试题 java_【干货】经典算法面试题代码实现-Java版

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

面试题3:二维数组中的查找

题目描述:一个二维数组,每一行从左到右递增,每一列从上到下递增.输入一个二维数组和一个整数,判断数组中是否含有整数。

publicclassFind{

publicstaticbooleanfind(int[][]array,intnumber){

if(array==null){

returnfalse;

}

intcolumn=array[0].length-1;

introw=0;

while(row=0){

if(array[row][column]==number){

returntrue;

}

if(array[row][column]>number){

column--;

}else{

row++;

}

}

returnfalse;

}

publicstaticvoidmain(Stringargs[]){

int[][]testarray=newint[4][4];

testarray[0][0]=1;

testarray[0][1]=2;

testarray[0][2]=8;

testarray[0][3]=9;

testarray[1][0]=2;

testarray[1][1]=4;

testarray[1][2]=9;

testarray[1][3]=12;

testarray[2][0]=4;

testarray[2][1]=7;

testarray[2][2]=10;

testarray[2][3]=13;

testarray[3][0]=6;

testarray[3][1]=8;

testarray[3][2]=11;

testarray[3][3]=15;

System.out.println(find(testarray,1));

}

}

面试题4:替换空格

题目:请实现一个函数,把字符串中的每个空格替换成“%20”。

publicclassReplaceBlank{

publicstaticvoidmain(Stringargs[]){

Strings="Wearehappy.";

System.out.println(replaceBlank(s));

}

publicStringreplaceBlank(Stringinput){

if(input==null)

returnnull;

StringBufferoutputBuffer=newStringBuffer();

for(inti=0;i

if(input.charAt(i)==''){

outputBuffer.append("%");

outputBuffer.append("2");

outputBuffer.append("0");

}else{

outputBuffer.append(String.valueOf(input.charAt(i)));

}

}

returnnewString(outputBuffer);

}

}

面试题5:从尾到头打印链表

题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。

方法一:非递归的方式

publicclassPrintListReverse{

publicstaticvoidmain(Stringargs[])

{

ListNodenode1=newListNode();

ListNodenode2=newListNode();

ListNodenode3=newListNode();

node1.data=1;

node2.data=2;

node3.data=3;

node1.next=node2;

node2.next=node3;

printListReversversinglytest=newprintListReversversingly();

test.printListReverse(node1);

}

publicstaticvoidprintListReverse(ListNodeheadNode){

Stackstack=newStack();

while(headNode!=null){

stack.push(headNode);

headNode=headNode.next;

}

while(!stack.isEmpty()){

System.out.println(stack.pop().data);

}

}

}

方法二:递归方式实现

publicclassPrintListReverse{

publicstaticvoidmain(Stringargs[]){

ListNodenode1=newListNode();

ListNodenode2=newListNode();

ListNodenode3=newListNode();

node1.data=1;

node2.data=2;

node3.data=3;

node1.next=node2;

node2.next=node3;

printListReversversinglytest=newprintListReversversingly();

test.printListReverse(node1);

}

publicstaticvoidprintListReverse(ListNodeheadNode){

if(headNode!=null){

if(headNode.next!=null){

printListReverse(headNode.next);

}

}

System.out.println(headNode.data);

}

}

面试题6:重建二叉树

题目描述:输入二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设前序遍历和中序遍历结果中都不包含重复的数字,例如输入的前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}重建出如图所示的二叉树。

publicclassBinaryTreeNode{

publicstaticintvalue;

publicBinaryTreeNodeleftNode;

publicBinaryTreeNoderightNode;

}

importjava.util.Arrays;

publicclassProblem6{

publicstaticvoidmain(String[]args)throwsException{

int[]preSort={1,2,4,7,3,5,6,8};

int[]inSort={4,7,2,1,5,3,8,6};

BinaryTreeNoderoot=constructCore(preSort,inSort);

}

publicstaticBinaryTreeNodeconstructCore(int[]preorder,int[]inorder)throwsException{

if(preorder==null||inorder==null){

returnnull;

}

if(preorder.length!=inorder.length){

thrownewException("长度不一样,非法的输入");

}

BinaryTreeNoderoot=newBinaryTreeNode();

for(inti=0;i

if(inorder[i]==preorder[0]){

root.value=inorder[i];

System.out.println(root.value);

root.leftNode=constructCore(Arrays.copyOfRange(preorder,1,i+1),Arrays.copyOfRange(inorder,0,i));

root.rightNode=constructCore(Arrays.copyOfRange(preorder,i+1,preorder.length),Arrays.copyOfRange(inorder,i+1,inorder.length));

}

}

returnroot;

}

}

面试题7:用两个栈实现队列

题目描述:用两个栈实现一个队列,实现对了的两个函数appendTail和deleteHead,分别完成在队列尾插入结点和在队列头部删除结点的功能。

publicclassProblem7{

privateStackstack1=newStack();

privateStackstack2=newStack();

publicvoidappendTail(Tt){

stack1.push(t);

}

publicTdeleteHead()throwsException{

if(stack2.isEmpty()){

while(!stack1.isEmpty()){

stack2.push(stack1.pop());

}

}

if(stack2.isEmpty()){

thrownewException("队列为空,不能删除");

}

returnstack2.pop();

}

publicstaticvoidmain(Stringargs[])throwsException{

Problem7p7=newProblem7<>();

p7.appendTail("1");

p7.appendTail("2");

p7.appendTail("3");

p7.deleteHead();

}

}

面试题8:旋转数组的最小数字

题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1.

publicclassProblem8{

publicstaticvoidmain(String[]args){

Problem8p8=newProblem8();

//int[]array={1,1,1,2,0};

//int[]array={3,4,5,1,2};

int[]array={1,0,1,1,1};System.out.println(p8.findMinNum(array));

}

publicIntegerfindMinNum(int[]array){

if(array==null){

returnnull;

}

intleftIndex=0;

intrightIndex=array.length-1;

intmid=0;

while(array[leftIndex]>=array[rightIndex]){

if(rightIndex-leftIndex<=1){

mid=rightIndex;

break;

}

mid=(leftIndex+rightIndex)/2;

if(array[leftIndex]==array[rightIndex]&&array[leftIndex]==array[mid]){

if(array[leftIndex+1]!=array[rightIndex-1]){

mid=array[leftIndex+1]

break;

}else{

leftIndex++;

rightIndex--;

}

}else{

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

leftIndex=mid;

else{

if(array[mid]<=array[rightIndex])

rightIndex=mid;

}

}

}

returnarray[mid];

}

}

面试题9:斐波那契数列

题目一:写一个函数,输入n,求斐波那契数列的第n项。

publicclassFibonacci{

publiclongfibonacci(intn){

longresult=0;

longpreOne=0;

longpreTwo=1;

if(n==0)

{

returnpreOne;

}

if(n==1)

{

returnpreTwo;

}

for(inti=2;i<=n;i++)

{

result=preOne+preTwo;

preOne=preTwo;

preTwo=result;

}

returnresult;

}

}

面试题10:二进制中1的个数

题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数。例如把9表示成二进制是1001;有2位是1,因此如果输入9,函数输出2.

publicclassProblem10{

publicstaticvoidmain(Stringargs[]){

Problem10test=newProblem10();

System.out.println(test.numberOf1(3));

}

publicintnumberOf1(intn){

intcount=0;

while(n!=0){

count++;

n=(n-1)&n;

}

returncount;

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值