Java求职面试准备之常见算法
最近在求职面试,整理一下常见面试算法:
对TestAlgorithms.java中方法的测试见JunitTestAlgorithms.java(引入了junit4)
1.TestAlgorithms.java
- package carl;
- import org.junit.Test;
- /**
- * 本类中总结了常用的几种算法
- * @author Administrator
- *
- */
- public class TestAlgorithms {
- /**
- * 插入排序
- * 插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小
- * 插入前面已经排序的文件中适当位置上,直到全部插入完为止。
- * @param list
- * @return
- */
- public void insertSort(int[] list){
- for(int i=1;i<list.length;i++){
- int tmp = list[i];
- int j=i-1;
- for(; j>=0 && list[j] >tmp; j--){
- list[j+1]=list[j];
- }
- list[j+1]=tmp;
- }
- }
- /**
- * 希尔排序:dk为1的时候就是插入排序
- * 希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;
- * 随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
- * @param list
- * @param start
- * @param dk
- */
- public void shellInsert(int[] list, int start, int dk) {
- int i,j;
- for(i = start+dk; i < list.length; i = i + dk){
- j = i-dk;
- int tmp = list[i];
- for (;j >= 0 && list[j] > tmp;j -= dk) {
- list[j + dk] = list[j];
- }
- list[j + dk] = tmp;
- }
- }
- /**
- * 冒泡排序
- * 它重复地走访过要排序的数列,一次比较两个元素,
- * 如果他们的顺序错误就把他们交换过来。走访数列的工作是
- * 重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
- * 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。
- * @param list
- */
- public void bubbleSort(int[] list) {
- for (int i = 0; i < list.length; i++) {
- for (int j = 0; j < list.length - i - 1; j++) {
- if (list[j] > list[j + 1]) {
- int temp = list[j];
- list[j] = list[j + 1];
- list[j + 1] = temp;
- }
- }
- }
- }
- // 字符串反转
- public String reverse(String str) {
- String str1 = "";
- for (int i = str.length(); i > 0; i--) {
- str1 += str.substring(i - 1, i);
- }
- return str1;
- }
- /**
- * 编码实现字符串转整型的函数(实现函数atoi的功能)。
- * 如将字符串”+123”->123, ”-0123”->-123,“123CS45”->123,“123.45CS”->123, “CS123.45”->0
- * @param str1
- * @return
- */
- public int str2Int(String str1) {
- char[] str = str1.toCharArray();
- int i = 0, sign = 1, value = 0;
- if (str != null && str[0] > '9' && str[0] < '0') {
- value = 0; // 如果第一个元素为字母,直接赋值零
- } else {
- if (str != null && str[0] == '-' || str[0] == '+') {
- // 判断是否存在符号位
- i = 1;
- sign = (str[0] == '-' ? -1 : 1);
- }
- for (; str[i] >= '0' && str[i] <= '9'; i++)
- value = value * 10 + (str[i] - '0');
- }
- return sign * value;
- }
- /**
- * 根据字节对字符串拆分,要注意若是GBK编码,则中文字符占用两个字节
- * 如“123我是谁”,拆分4个字节,程序要求的结果为“123我”,而不是“123?”
- * @param str
- */
- public String splitChinese(String str,int index){
- String tmp = "";
- for(int i=1;i<=str.length();i++){
- tmp = str.substring(0, i);
- if(tmp.getBytes().length >= index){
- return tmp;
- }
- }
- System.out.println(tmp.getBytes().length);
- return "";
- }
- }
2.JunitTestAlgorithms.java
- package carl;
- import java.io.UnsupportedEncodingException;
- import org.junit.Test;
- public class JunitTestAlgorithms {
- TestAlgorithms ta = new TestAlgorithms();
- public void pintList(int[] target){
- int len = target.length;
- System.out.println("length:"+len);
- for(int i = 0; i < len; i++){
- System.out.print(target[i]+ (i==(len-1)?"":","));
- }
- }
- @Test
- public void insertSortTest(){
- int[] list = {1,2,3,4,7,5,6,10,22};
- ta.insertSort(list);
- System.out.println("\n------------insertSortTest-----------");
- this.pintList(list);
- }
- @Test
- public void shellInsertTest(){
- int[] list = {1,2,3,4,7,5,6,10,22};
- int i = 0, w = 2;
- while (i < w) {
- ta.shellInsert(list, i, w);
- i++;
- }
- ta.shellInsert(list, 0, 1);
- System.out.println("\n-----------shellInsertTest-----------");
- this.pintList(list);
- }
- @Test
- public void bubbleSortTest(){
- int[] list = {1,2,3,4,7,5,6,10,22};
- ta.bubbleSort(list);
- System.out.println("\n------------bubbleSortTest---------");
- this.pintList(list);
- }
- @Test
- public void reverseTest(){
- String str = "abcdefg";
- String tmp = ta.reverse(str);
- System.out.println("\n----------reverseTest------------");
- System.out.println(tmp);
- }
- @Test
- public void str2IntTest(){
- int str2Int = ta.str2Int("-123.4CS4546");
- System.out.println("\n----------str2IntTest------------");
- System.out.println(str2Int);
- }
- @Test
- public void splitChineseTest(){
- String str="123我是谁";
- System.out.println("\n----------splitChineseTest------------");
- try {
- //因为我的java文件编码是GBK,所以getBytes()默认的编码是GBK
- System.out.println("\"123我是谁\".getBytes(\"GBK\").length:"+str.getBytes("GBK").length);
- System.out.println("\"123我是谁\".getBytes().length:"+str.getBytes().length);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- String resultStr = ta.splitChinese(str,4);
- System.out.println(resultStr);
- }
- }
输出结果:
----------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求职面试准备之常见算法的更多相关文章
- 常见算法合集[java源码+持续更新中...]
一.引子 本文搜集从各种资源上搜集高频面试算法,慢慢填充...每个算法都亲测可运行,原理有注释.Talk is cheap,show me the code! 走你~ 二.常见算法 2.1 判断单向链 ...
- Android开发面试经——3.常见Java基础笔试题
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...
- java异常面试常见题目
在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...
- 【搞定Jvm面试】 Java 内存区域揭秘附常见面试题解析
本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错 ...
- 大公司面试经典数据结构与算法题C#/Java解答
几个大公司(IBM.MicroSoft and so on)面试经典数据结构与算法题C#解答 1.链表反转 我想到了两种比较简单的方法 第一种是需要开一个新的链表,将原链表的元素从后到前的插入到新链表 ...
- 面试十大常见Java String问题
本文介绍Java中关于String最常见的10个问题: 1. 字符串比较,使用 "==" 还是 equals() ?简单来说, "==" 判断两个引用的是不是同 ...
- 两年Java的面试经验
前言:从过年前就萌生出要跳槽的想法,到过年来公司从3月初提出离职到23号正式离职,上班的时间也出去面试过几家公司,后来总觉的在职找工作总是得请假,便决心离职后找工作.到4月10号找到了一家互联网公司成 ...
- Android开发面试经——5.常见面试官提问Android题①
版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客: http://blog.csdn.net/f ...
- java开发面试问题
Java面试题:java的垮平台原理 为什么要跨平台使用????? 其实说白了就是个操作系统支持的指令集是不一样的.我们的程序需要再不同的操作系统上运行这些代码. 但是不要说jvm是跨平台的,而真正跨 ...
随机推荐
- win8或win8.1修改注册表失败的原因
win8 and win8.1 modify the registry need compiled to be different versions according to the os bits.
- oracle通过DBlink连接mysql(MariaDB)
1.安装先装 mysql-connector-odbc(或 mariadb-connector-odbc )和unixODBChttps://downloads.mariadb.org/mariadb ...
- Android多线程异步处理:AsyncTask 的实现原理
AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法.注意继承时需要设定三个泛型P ...
- Socket WSAAsyncSelect模型
::WSAAsyncSelect(sListen, hWnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE); 自定义 WM_SOCKET消息 #include "../co ...
- ES6生成器基础
ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...
- SQLServer、MySQL、Oracle语法差异小集锦
一.差异集锦 在建表的时候,只有自增的语法不同. 下面给出3种数据库通用的建表与初始化测试语句: CREATE TABLE Country( Id int PRIMARY KEY, Name ) ); ...
- CThreadPool
class CThreadPool { public: template <typename T> static void QueueUserWorkItem(void (T::*func ...
- Linux之档案管理
1:档案类型[1] d :目录 -:档案 l:链接档 b:装置文件中可存储接口设备 c:装置文件中串行设备,例如:键盘,鼠标 2:RWX: R:read (可读),W:write(可写),X:excu ...
- 优秀的富文本编辑器 Kindeditor
<textarea name="txtbody" style="width:100%;height:320px;" > {$article.txt} ...
- Longest Common Prefix [LeetCode 14]
1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...