总有一些题,超越了岁月,即便是经过了新框架的层层迭代,它依然散发着令人回味无穷的味道。下面的几个笔试题目,是JAVA面试中经常遇见的,大家一定要牢记于心,可别复习到了到时候又说不出来。我就吃过这种亏,不说啦,下面来看题目。

二维数组中的查找

  • 面试题

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

  • @代码

public class Test7 {

public static void main(String[] args) {int[][] array = new int[][] {{1,2},{2,3},{3,4}};boolean find1 = find(3, array);boolean find2 = find(8, array);       System.out.println(find1); // 输出true      System.out.println(find2); // 输出 false

}

/**     * @param target  * @param array   * @return    */public static boolean find(int target, int [][] array) {int row = 0;int col = array[0].length-1;

while(row<array.length && col>=0){if(array[row][col] == target)return true;else if(array[row][col] > target)            col-=1;else            row+=1;}return false;}

}
  • 5

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

链表题

  • 面试题

输入一个链表,按链表从尾到头的顺序返回一个ArrayList

  • 代码

class ListNode {

int val;            ListNode next = null;ListNode(int val) {this.val = val;   }}

public class Test8 {

public static void main(String[] args) {        ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(new ListNode(10));        System.out.println(printListFromTailToHead.size());for (Integer integer : printListFromTailToHead) {            System.out.println(integer);}

}

/**    *     * @param listNode    * @return    */public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

            ArrayList<Integer> arr = new ArrayList<Integer>();           ListNode p = listNode;           ArrayList<Integer> stack = new ArrayList<Integer>();while(p!=null){               stack.add(p.val);             p = p.next;}int n = stack.size();for(int i=n-1;i>=0;i--){                arr.add(stack.get(i));}return arr;}

}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

队列题

  • 面试题

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

  • 代码

public class Test9 {

static Stack<Integer> stack1 = new Stack<Integer>();static Stack<Integer> stack2 = new Stack<Integer>();

public static void main(String[] args) {push(1);push(2);push(3);          System.out.println(stack1.size());            System.out.println(stack2.size());pop();            System.out.println(stack1.size());            System.out.println(stack2.size());}

public static void push(int node) {         stack1.push(node);}

/**     * pop操作 复杂     * @return     */public static int pop() {int temp;

while(!stack1.empty()){            temp = stack1.pop();            stack2.push(temp);}

int res = stack2.pop();while(!stack2.empty()){            temp = stack2.pop();            stack1.push(temp);}return res;}}

数组题

  • 面试题

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

  • 代码

public class Test10 {public static void main(String[] args) {int[] array = new int[] {1,2,4,3,5,6,0,-1,-100};int minNumberInRotateArray = minNumberInRotateArray(array );       System.out.println(minNumberInRotateArray);}

public static int minNumberInRotateArray(int [] array) {if(array.length==0){return 0;}for(int i=0;i<array.length-1;i++){if(array[i] > array[i+1]){return array[i+1];}}return array[0];}}

斐波那契数列问题

  • 面试题

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项 [科普] 斐波那契数列指的是这样一个数列 0, 1,1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368…

  • 代码

public class Test11 {public static void main(String[] args) {int fibonacci = fibonacci(10);       System.out.println(fibonacci);}

public static int fibonacci(int n) {if (n<=0)return 0;

int a=1,b = 1;int temp;for(int i=2;i<n;i++){             temp = a;                a = b;               b = temp + b;}return b;}}

青蛙上台阶问题

  • 面试题

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)

  • 代码

public class Test12 {public static void main(String[] args) {int jumpFloor = jumpFloor(18);     System.out.println(jumpFloor);}

public static int jumpFloor(int target) {if(target <= 0)return 0;if(target <= 2)return target;int a=1,b=2;int temp;for(int i=3;i<=target;i++){                    temp = a;                    a = b;                   b += temp;}return b;}}

变态青蛙跳台阶问题

  • 面试

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

  • 代码

public class Test13 {public static void main(String[] args) {int jumpFloorII = jumpFloorII(18);      System.out.println(jumpFloorII);}

public static int jumpFloorII(int target) {if(target<=0)return 0;int sumPath = 0;int path = 0;for(int i=0;i<target;i++){             path = sumPath + 1;             sumPath = sumPath * 2 + 1;}return path;}}

矩形覆盖问题

  • 面试题

我们可以用 2×1 的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2×n的大矩形,总共有多少种方法?

  • 代码

public class Test14 {

public static void main(String[] args) {int rectCover = rectCover(10);     System.out.println(rectCover);}

public static int rectCover(int target) {if(target <= 0)return 0;if(target <= 2)return target;int a=1,b=2;int temp;for(int i=3;i<=target;i++){           temp = a;           a = b;           b += temp;}return b;}

}
这些题目虽然看上去比较简单,但是蕴含了丰富的编程思想,大家可以经常练习,提高思维能力。这是面试题中经常出现的,还有一些类似的题目,大家有的话也可以提出来,我们一起进步!

欢迎关注,转发朋友圈噢

 

你“在看”我吗?

mopso算法代码程序_JAVA程序员的必杀技,面试中常考的8个经典算法题,过程精妙,代码精炼...相关推荐

  1. 机器学习(Machine Learning)、深度学习(Deep Learning)、NLP面试中常考到的知识点和代码实现

    网址:https://github.com/NLP-LOVE/ML-NLP 此项目是机器学习(Machine Learning).深度学习(Deep Learning).NLP面试中常考到的知识点和代 ...

  2. 机器学习面试中常考的知识点,附代码实现(二)

    决策树 什么是决策树1.决策树的基本思想 其实用一下图片能更好的理解LR模型和决策树模型算法的根本区别,我们可以思考一下一个决策问题:是否去相亲,一个女孩的母亲要给这个女海介绍对象. 大家都看得很明白 ...

  3. Android面试中常问的几种简单算法(两数之和、开灯、上楼梯、柠檬水、找最多的数)

    大家好,最近在面试,大小公司可能都会问一两个算法,来测试面试者的算法能力.本篇博客从笔者被问到的几个简单算法来给大家分享一下,(面试官会根据面试者的能力,提问不同难度,这篇的分享是比较简单的,大神可忽 ...

  4. 【2023校招刷题】笔试及面试中常考知识点、手撕代码总结

    文章目录 一.笔试/面试常考知识点 二.面试常考手撕代码 2.1.基本电路设计 2.2.复杂电路设计 2.3.跨时钟域设计 一.笔试/面试常考知识点 奇.偶.小数分频 [Verilog基础]分频器实现 ...

  5. (面经总结)一篇文章带你整理面试过程中常考的九大排序算法

    文章目录 一.二分插入排序 1. 原理 2. 代码 二.冒泡排序 1. 原理 2. 代码 三.插入排序算法 1. 原理 2. 代码 四.快速排序算法 1. 原理 2. 代码 五.希尔排序 1. 原理 ...

  6. 程序员如何快速准备面试中的算法 - 结构之法

    准备面试.学习算法,特别推荐最新出版的我的新书<编程之法:面试和算法心得>,已经上架京东等各大网店 前言 我决定写篇短文,即为此文.之所以要写这篇文章,缘于微博上常有朋友询问,要毕业找工作 ...

  7. 程序员如何快速准备面试中的算法

    程序员如何快速准备面试中的算法 准备面试.学习算法,特别推荐最新出版的 新书<编程之法:面试和算法心得>,已经上架 京东等各大网店 前言 我决定写篇短文,即为此文.之所以要写这篇文章,缘于 ...

  8. 作为一名Java程序员,这些Spring知识点面试官常考

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 丸纸 来源 | 极客时间 毋庸置疑,Spring 早已成为 Java 后端开发事实 ...

  9. 面试常考的常用数据结构与算法

    面试常考的常用数据结构与算法 数据结构与算法,这个部分的内容其实是十分的庞大,要想都覆盖到不太容易.在校学习阶段我们可能需要对每种结构,每种算法都学习,但是找工作笔试或者面试的时候,要在很短的时间内考 ...

最新文章

  1. BZOJ2588 Count on a tree 【树上主席树】
  2. Android 应用Crash 后自动重启
  3. DB2更改数据文件路径
  4. Linux Swap分区设定
  5. sobel prewitt算法 模板加权模糊的解释 + 两类边缘下的二阶导数值
  6. 安卓开发之如何利用Intent对象,实现Activity和另一个Activity之间的跳转
  7. Xilinx FIFO IP核使用
  8. Apache 中 .htaccess 文件设置技巧16则
  9. [zz]KVM 虚拟机故障排除一例
  10. python往list中添加tuple
  11. c语言 程序设计 题库答案 p,c语言 程序设计 题库答案 p
  12. 数据库索引失效与判断是否命中索引
  13. Oracle MySQL sql 列转行 union all 实现
  14. JS实现div随鼠标移动练习
  15. vue路由守卫的执行顺序
  16. 【C认证】对标名企技术标准,大厂不是梦
  17. 荣耀最强拍照手机诞生:荣耀20系列塑造的“潮流科技宇宙”
  18. 轻松下载网页视频(.flv)||B站高清视频(.mp4)
  19. TP-LINK无线路由器配置解读
  20. POI解析Word批注信息

热门文章

  1. java编码-多重(乱码)
  2. 3.4_函数_Function_Part_2
  3. Go Language 开发环境搭建
  4. 【剑指Offer】俯视50题之31 - 40题
  5. Error:Execution failed for task ':app:clean'.
  6. HttpClient使用方法(包括POST文件)
  7. 【DFS】 HDU 3298 Contestants
  8. 菜鸟学习JavaScript小实验之函数引用
  9. 另我模糊的URL 重写
  10. android签名的应用-- 禁止未经授权签名的apk安装