最近在求职面试,整理一下常见面试算法:

对TestAlgorithms.java中方法的测试见JunitTestAlgorithms.java(引入了junit4)

1.TestAlgorithms.java

  1. package carl;
  2.  
  3. import org.junit.Test;
  4.  
  5. /**
  6. * 本类中总结了常用的几种算法
  7. * @author Administrator
  8. *
  9. */
  10. public class TestAlgorithms {
  11.  
  12. /**
  13. * 插入排序
  14. * 插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小
  15. * 插入前面已经排序的文件中适当位置上,直到全部插入完为止。
  16. * @param list
  17. * @return
  18. */
  19. public void insertSort(int[] list){
  20. for(int i=1;i<list.length;i++){
  21. int tmp = list[i];
  22. int j=i-1;
  23. for(; j>=0 && list[j] >tmp; j--){
  24. list[j+1]=list[j];
  25. }
  26. list[j+1]=tmp;
  27. }
  28.  
  29. }
  30.  
  31. /**
  32. * 希尔排序:dk为1的时候就是插入排序
  33. * 希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;
  34. * 随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
  35. * @param list
  36. * @param start
  37. * @param dk
  38. */
  39. public void shellInsert(int[] list, int start, int dk) {
  40. int i,j;
  41. for(i = start+dk; i < list.length; i = i + dk){
  42. j = i-dk;
  43. int tmp = list[i];
  44.  
  45. for (;j >= 0 && list[j] > tmp;j -= dk) {
  46. list[j + dk] = list[j];
  47. }
  48. list[j + dk] = tmp;
  49. }
  50.  
  51. }
  52.  
  53. /**
  54. * 冒泡排序
  55. * 它重复地走访过要排序的数列,一次比较两个元素,
  56. * 如果他们的顺序错误就把他们交换过来。走访数列的工作是
  57. * 重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
  58. * 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。
  59. * @param list
  60. */
  61. public void bubbleSort(int[] list) {
  62. for (int i = 0; i < list.length; i++) {
  63. for (int j = 0; j < list.length - i - 1; j++) {
  64. if (list[j] > list[j + 1]) {
  65. int temp = list[j];
  66. list[j] = list[j + 1];
  67. list[j + 1] = temp;
  68. }
  69. }
  70. }
  71. }
  72.  
  73. // 字符串反转
  74. public String reverse(String str) {
  75. String str1 = "";
  76. for (int i = str.length(); i > 0; i--) {
  77. str1 += str.substring(i - 1, i);
  78. }
  79. return str1;
  80. }
  81.  
  82. /**
  83. * 编码实现字符串转整型的函数(实现函数atoi的功能)。
  84. * 如将字符串”+123”->123, ”-0123”->-123,“123CS45”->123,“123.45CS”->123, “CS123.45”->0
  85. * @param str1
  86. * @return
  87. */
  88. public int str2Int(String str1) {
  89. char[] str = str1.toCharArray();
  90. int i = 0, sign = 1, value = 0;
  91. if (str != null && str[0] > '9' && str[0] < '0') {
  92. value = 0; // 如果第一个元素为字母,直接赋值零
  93. } else {
  94. if (str != null && str[0] == '-' || str[0] == '+') {
  95. // 判断是否存在符号位
  96. i = 1;
  97. sign = (str[0] == '-' ? -1 : 1);
  98. }
  99. for (; str[i] >= '0' && str[i] <= '9'; i++)
  100. value = value * 10 + (str[i] - '0');
  101. }
  102. return sign * value;
  103. }
  104.  
  105. /**
  106. * 根据字节对字符串拆分,要注意若是GBK编码,则中文字符占用两个字节
  107. * 如“123我是谁”,拆分4个字节,程序要求的结果为“123我”,而不是“123?”
  108. * @param str
  109. */
  110. public String splitChinese(String str,int index){
  111. String tmp = "";
  112. for(int i=1;i<=str.length();i++){
  113. tmp = str.substring(0, i);
  114. if(tmp.getBytes().length >= index){
  115. return tmp;
  116. }
  117. }
  118. System.out.println(tmp.getBytes().length);
  119.  
  120. return "";
  121. }
  122.  
  123. }

2.JunitTestAlgorithms.java

  1. package carl;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4.  
  5. import org.junit.Test;
  6.  
  7. public class JunitTestAlgorithms {
  8. TestAlgorithms ta = new TestAlgorithms();
  9.  
  10. public void pintList(int[] target){
  11. int len = target.length;
  12. System.out.println("length:"+len);
  13. for(int i = 0; i < len; i++){
  14. System.out.print(target[i]+ (i==(len-1)?"":","));
  15. }
  16. }
  17.  
  18. @Test
  19. public void insertSortTest(){
  20. int[] list = {1,2,3,4,7,5,6,10,22};
  21. ta.insertSort(list);
  22. System.out.println("\n------------insertSortTest-----------");
  23. this.pintList(list);
  24. }
  25.  
  26. @Test
  27. public void shellInsertTest(){
  28. int[] list = {1,2,3,4,7,5,6,10,22};
  29. int i = 0, w = 2;
  30. while (i < w) {
  31. ta.shellInsert(list, i, w);
  32. i++;
  33. }
  34. ta.shellInsert(list, 0, 1);
  35. System.out.println("\n-----------shellInsertTest-----------");
  36. this.pintList(list);
  37. }
  38.  
  39. @Test
  40. public void bubbleSortTest(){
  41. int[] list = {1,2,3,4,7,5,6,10,22};
  42. ta.bubbleSort(list);
  43. System.out.println("\n------------bubbleSortTest---------");
  44. this.pintList(list);
  45. }
  46.  
  47. @Test
  48. public void reverseTest(){
  49. String str = "abcdefg";
  50. String tmp = ta.reverse(str);
  51. System.out.println("\n----------reverseTest------------");
  52. System.out.println(tmp);
  53. }
  54.  
  55. @Test
  56. public void str2IntTest(){
  57. int str2Int = ta.str2Int("-123.4CS4546");
  58. System.out.println("\n----------str2IntTest------------");
  59. System.out.println(str2Int);
  60. }
  61.  
  62. @Test
  63. public void splitChineseTest(){
  64. String str="123我是谁";
  65. System.out.println("\n----------splitChineseTest------------");
  66. try {
  67. //因为我的java文件编码是GBK,所以getBytes()默认的编码是GBK
  68. System.out.println("\"123我是谁\".getBytes(\"GBK\").length:"+str.getBytes("GBK").length);
  69. System.out.println("\"123我是谁\".getBytes().length:"+str.getBytes().length);
  70. } catch (UnsupportedEncodingException e) {
  71. e.printStackTrace();
  72. }
  73. String resultStr = ta.splitChinese(str,4);
  74. System.out.println(resultStr);
  75. }
  76.  
  77. }

输出结果:

----------splitChineseTest------------
"123我是谁".getBytes("GBK").length:9
"123我是谁".getBytes().length:9
123我

------------insertSortTest-----------
length:9
1,2,3,4,5,6,7,10,22
----------str2IntTest------------
-123

-----------shellInsertTest-----------
length:9
1,2,3,4,5,6,7,10,22
----------reverseTest------------
gfedcba

------------bubbleSortTest---------
length:9
1,2,3,4,5,6,7,10,22

Java求职面试准备之常见算法的更多相关文章

  1. 常见算法合集[java源码+持续更新中...]

    一.引子 本文搜集从各种资源上搜集高频面试算法,慢慢填充...每个算法都亲测可运行,原理有注释.Talk is cheap,show me the code! 走你~ 二.常见算法 2.1 判断单向链 ...

  2. Android开发面试经——3.常见Java基础笔试题

      Android开发(29)  版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...

  3. java异常面试常见题目

    在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...

  4. 【搞定Jvm面试】 Java 内存区域揭秘附常见面试题解析

    本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错 ...

  5. 大公司面试经典数据结构与算法题C#/Java解答

    几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...

  6. 面试十大常见Java String问题

    本文介绍Java中关于String最常见的10个问题: 1. 字符串比较,使用 "==" 还是 equals() ?简单来说, "==" 判断两个引用的是不是同 ...

  7. 两年Java的面试经验

    前言:从过年前就萌生出要跳槽的想法,到过年来公司从3月初提出离职到23号正式离职,上班的时间也出去面试过几家公司,后来总觉的在职找工作总是得请假,便决心离职后找工作.到4月10号找到了一家互联网公司成 ...

  8. Android开发面试经——5.常见面试官提问Android题①

    版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客: http://blog.csdn.net/f ...

  9. java开发面试问题

    Java面试题:java的垮平台原理 为什么要跨平台使用????? 其实说白了就是个操作系统支持的指令集是不一样的.我们的程序需要再不同的操作系统上运行这些代码. 但是不要说jvm是跨平台的,而真正跨 ...

随机推荐

  1. win8或win8.1修改注册表失败的原因

    win8 and win8.1 modify the registry need compiled to be different versions according to the os bits.

  2. oracle通过DBlink连接mysql(MariaDB)

    1.安装先装 mysql-connector-odbc(或 mariadb-connector-odbc )和unixODBChttps://downloads.mariadb.org/mariadb ...

  3. Android多线程异步处理:AsyncTask 的实现原理

    AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法.注意继承时需要设定三个泛型P ...

  4. Socket WSAAsyncSelect模型

    ::WSAAsyncSelect(sListen, hWnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE); 自定义 WM_SOCKET消息 #include "../co ...

  5. ES6生成器基础

    ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...

  6. SQLServer、MySQL、Oracle语法差异小集锦

    一.差异集锦 在建表的时候,只有自增的语法不同. 下面给出3种数据库通用的建表与初始化测试语句: CREATE TABLE Country( Id int PRIMARY KEY, Name ) ); ...

  7. CThreadPool

    class CThreadPool { public: template <typename T> static void QueueUserWorkItem(void (T::*func ...

  8. Linux之档案管理

    1:档案类型[1] d :目录 -:档案 l:链接档 b:装置文件中可存储接口设备 c:装置文件中串行设备,例如:键盘,鼠标 2:RWX: R:read (可读),W:write(可写),X:excu ...

  9. 优秀的富文本编辑器 Kindeditor

    <textarea name="txtbody" style="width:100%;height:320px;" > {$article.txt} ...

  10. Longest Common Prefix [LeetCode 14]

    1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...