最近投了風火山林的高級java工程師職位,對於我這種剛畢業的菜鳥來講能挺到第三輪,可能對你們來講還算能夠,但我內心面對本身的要求可不是那樣的。第三輪是算法題,就是發幾個題目給你兩天時間作完,實際上我一天就作完了,只是在算法的複雜度上沒有考慮,我想這也是被刷下來的緣由之一吧 。java
題目以下:面試
這是工具類:
package www.work.service;
import java.util.List;
public interface Work_One {
public List<Integer> exec(int n,Integer[] data)throws Exception;
}
算法
package www.work.service.impl;
import java.util.ArrayList;
import java.util.List;
import www.work.service.Work_One;
import www.work.util.CheckArray;
import www.work.util.Work_OneQuicksort;
/**題目描述:1. 請實現一個函數:湊14;輸入不少個整數(1<=數值<=13),
* 任意兩個數相加等於14就能夠從數組中刪除這兩個數,求剩餘數(按由小到大排列);
* 好比: 輸入數組[9,1,9,7,5,13], 輸出數組[7,9]
*
* @author 陳硯庭
*
*/
public class Work_OneQSort implements Work_One{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Work_OneQuicksort q=new Work_OneQuicksort();
Work_One qSoft=new Work_OneQSort();
Integer[] data={1,2,13,2,13,1,4,6,5,7,4,7,3};
q.setData(data);
q.sort(0, data.length-1);
q.display();
List<Integer> lists=null;
try {
if(CheckArray.checkArray(data))
lists = qSoft.exec(14, data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(lists);
}
public List<Integer> exec(int n,Integer[] data)throws Exception{
if(data==null){
throw new NullPointerException();
}
if(n<0){
throw new IllegalArgumentException();
}
Integer temp=n%2==0? n/2:n/2+1;
if(data[0]>temp||data[data.length-1]<temp){
return null;
}
for(int i=0;i<data.length;i++){
if(data[i]>temp){
data[i]=n;
}else{
for(int j=i;j<data.length;j++){
if(data[i]+data[j]==n){
data[i]=0;
data[j]=0;
}
}
}
}
return getResult(n,data);
}
/**
* 把數組中多餘的元素剔除掉
* @param n
* @param arg0
* @return
*/
private List<Integer> getResult(int n,Integer[] arg0){
List<Integer> lists=new ArrayList<Integer>();
for(int i=0;i<arg0.length;i++){
if(!(arg0[i]==0||arg0[i]==n)){
lists.add(arg0[i]);
}
}
return lists;
}
}
編程
package www.work.service;
import java.util.List;
public interface Work_Two {
/**
* 根據一維座標獲取重疊部分
* @param array
* @return
* @throws Exception
*/
public List<List<Double>> getSegment(Double[][] array)throws Exception;
}
package www.work.service.impl;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import www.work.service.Work_Two;
/**
* 問題描述:2. 請實現一個函數:線段重疊; 輸入多個一維線段,
* 求出這些線段相交的全部區域(也用線段表示);
* 一條線段用兩個值表示(x0,x1), 其中x1>x0;
*好比: 輸入線段數組[(2,4),(1.5,6),(0.5,3.5),(5,7),(7.5,9)], 輸出線段數組[(1.5,4),(5,6)]
* @author 陳硯庭
*
*/
public class Work_TwoSegment implements Work_Two {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Double[][] array={{2.0,4.0},{1.5,6.0},{0.5,3.5},{5.0,7.0},{7.5,9.0}};
Double[][] arrayInt={{2.0,4.0},{4.0,6.0},{6.0,7.0},{7.0,7.0},{7.5,9.0}};
Double temp[][]={{1.5,6.0},{2.4,7.8}};
Work_Two two=new Work_TwoSegment();
System.out.println(two.getSegment(array));
}
public List<List<Double>> getSegment(Double[][] array) throws Exception {
if (array == null) {
throw new NullPointerException();
}
//將二維數組轉化爲一維數組
Double[] temp = new Double[array.length * 2];
int[] count = new int[array.length * 2];//存放點的基數的數組,基數越大表明該點被包含的區間越多
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
temp[i * array[i].length + j] = array[i][j];
}
}
Arrays.sort(temp);
Double x = 0.0;
Double y = 0.0;
for (int i = 0; i < array.length; i++) {
x = array[i][0];
for (int j = 1; j < array[i].length; j++) {
y = array[i][j];
for (int k = 0; k < temp.length; k++) {
if (temp[k] >= x && temp[k] < y)
++count[k];
}
}
}
List<List<Double>> resultList = new LinkedList<List<Double>>();
List<Double> list = null;
int flag = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] > 1 && flag == 0) {
flag = 1;
list = new LinkedList<Double>();
list.add(temp[i]);
} else if (count[i] == 1 && flag == 1) {
flag = 0;
list.add(temp[i]);
resultList.add(list);
list = null;
}
}
return resultList;
}
}
數組
package www.work.service;
import java.util.ArrayList;
public interface Work_Three {
public ArrayList<ArrayList<Integer>> getLongest(Integer[] array) throws Exception;
}
package www.work.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import www.work.service.Work_Three;
import www.work.util.CheckArray;
/**
* 題目描述:3. 請實現一個函數:最長順子;輸入不少個整數(1<=數值<=13),
* 返回其中可能組成的最長的一個順子(順子中數的個數表明順的長度);
* 其中數字1也能夠表明14; 順子包括單順\雙順\3順;單順的定義是連續5個及以上連續的數,
* 好比1,2,3,4,五、3,4,5,6,7,8和10,11,12,13,1等;雙順的定義是連續3個及以上連續的對(對:兩個相同的數被稱爲對),
* 好比1,1,2,2,3,三、4,4,5,5,6,6,7,7和11,11,12,12,13,13,1,1等;3順的定義是連續2個及以上連續的3張(3張:3個相同的數被稱爲3張),
* 好比1,1,1,2,2,二、3,3,3,4,4,4,5,5,5,6,6,6和13,13,13,1,1,1等等;
*好比:輸入數組[1,5,2,3,4,4,5,9,6,7,2,3,3,4], 輸出數組[2,2,3,3,4,4,5,5]
* @author 陳硯庭
*
*/
public class Work_ThreeSequence implements Work_Three{
private int[] tempCount=null;
public static void main(String[] args) {
Work_Three longest=new Work_ThreeSequence();
Integer[] array = { 1, 5, 2, 3, 4, 4, 5, 9, 6, 7, 2, 3, 3, 4 ,1,13,13,13,11,11,11,12,12,12,1,2};
try {
if(CheckArray.checkArray(array))
System.out.println(longest.getLongest(array));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<ArrayList<Integer>> getLongest(Integer[] array)throws Exception{
if(array==null){
throw new NullPointerException();
}
Arrays.sort(array);//對數組進行排序
ArrayList<ArrayList<Integer>> listOne=null;
ArrayList<ArrayList<Integer>> listTwo=null;
ArrayList<ArrayList<Integer>> listThree=null;
try{
listOne = getLongest(array, 1);
listTwo = getLongest(array, 2);
listThree= getLongest(array, 3);
int list1Length=listOne.get(0).size();
int list2Length=listTwo.get(0).size();
int list3Length=listThree.get(0).size();
if(list2Length>list1Length){
listOne=listTwo;
}
if(list3Length>list1Length){
listOne=listThree;
}
}catch(Exception e){
throw new NullPointerException();
}
return listOne;
}
/**
* 得到數組中的順子
* @param array
* @param count
* @return
*/
private ArrayList<ArrayList<Integer>> getLongest(Integer[] array, int count) throws NullPointerException,IllegalArgumentException{
if(array==null){
throw new NullPointerException();
}
if(count<1){
throw new IllegalArgumentException();
}
tempCount=new int[14];
//取得每一個數字在數組中出現的個數
for(int i=0;i<array.length;i++){
tempCount[array[i]]++;
}
ArrayList<Integer> longest = new ArrayList<Integer>();//記錄最長的順子
ArrayList<Integer> temp = new ArrayList<Integer>();//記錄當前的順子
ArrayList<ArrayList<Integer>> longests=new ArrayList<ArrayList<Integer>>();//記錄最長的順子,不分大小
for (int i = 0; i < array.length;i+=tempCount[array[i]]) {
int start = array[i];
if (getCount(start, count)) {
for (int j = 0; j < count; j++) {
temp.add(start);
}
} else {
continue;
}
while (start <= 13) {
if (getCount(start + 1, count)) {
for (int j = 0; j < count; j++) {
if (start + 1 == 14) {
temp.add(1);
} else {
temp.add(start + 1);
}
}
} else {
break;
}
start++;
}
if (temp.size()>longest.size()) {
longest = (ArrayList<Integer>) temp.clone();
longests.clear();
longests.add(longest);
}else if(temp.size()==longest.size()){
if(!temp.equals(longest)){
longests.add(temp);
}
}
temp = new ArrayList<Integer>();
}
return longests;
}
/**
* 在數組中查找n是否出現count次
* @param n 尋找的次數
* @param count 出現的次數
* @return
*/
private boolean getCount(int n, int count) {
if (n == 14) {
n = 1;
}
if(tempCount[n]>=count){
return true;
}else{
return false;
}
}
}
多線程
package www.work.util;
public class Work_FourArrayFile {
private int index = 0;// 數組長度
private String[] files = null;
public synchronized void add(String file) {
index++;
String[] tempFiles = new String[index];
if (files == null) {
tempFiles[0] = file;
} else {
tempFiles[tempFiles.length - 1] = file;
System.arraycopy(files, 0, tempFiles, 0, files.length);
}
files = tempFiles.clone();
}
public synchronized void remove(String file) {
if (index < 0) {
throw new IllegalArgumentException();
}
index--;
String[] tempFiles = new String[index];
int indexTemp = 0;
for (int i = 0; i < files.length; i++) {
if (files[i] == file) {
indexTemp = i + 1;
continue;
}
}
if (indexTemp != 0) {
if (indexTemp != 1) {
System.arraycopy(files, 0, tempFiles, 0, indexTemp - 1);
}
if (indexTemp != files.length) {
System.arraycopy(files, indexTemp, tempFiles, indexTemp - 1,
files.length - indexTemp);
}
files = tempFiles.clone();
}
}
public int getIndex() {
return index;
}
public String[] getFiles() {
return files;
}
/*
* public void clear() { files=null; }
*/
public static void main(String... args) {
Work_FourArrayFile work_FourArrayFile = new Work_FourArrayFile();
int[] temp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] index = new int[9];
System.arraycopy(temp, 1, index, 0, 9);
for (int i = 0; i < index.length; i++) {
System.out.println(index[i]);
}
work_FourArrayFile.add(100 + "");
work_FourArrayFile.add(101 + "");
work_FourArrayFile.add(102 + "");
work_FourArrayFile.add(103 + "");
work_FourArrayFile.add(104 + "");
work_FourArrayFile.add(105 + "");
work_FourArrayFile.add(106 + "");
work_FourArrayFile.add(107 + "");
work_FourArrayFile.add(108 + "");
work_FourArrayFile.add(109 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(100 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(101 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(102 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(103 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(104 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(105 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
work_FourArrayFile.remove(106 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(107 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(108 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println();
work_FourArrayFile.remove(109 + "");
for (String i : work_FourArrayFile.getFiles())
System.out.print(i + "\t");
System.out.println("sdfd");
}
}
package www.work.util;
import java.util.HashMap;
import java.util.Map;
public class Work_Fourutil {
private Work_Fourutil(){}
private static Map<String, Long> maps = new HashMap<String, Long>();
public static void put(String arg0,Long arg1){
maps.put(arg0, arg1);
}
public static Map<String, Long> getMaps() {
return maps;
}
}
package www.work.service;
import java.util.Map;
public interface Work_Four {
/**
* 從指定目錄下讀取文件
* @param path
* @return
* @throws Exception
*/
public Map<String, Long> readProject(String path)throws Exception;
}
package www.work.service.impl;
import java.util.Map;
import www.work.service.Work_Four;
import www.work.util.Work_FourArrayFile;
import www.work.util.Work_Fourutil;
/**
* 4.請設計一個程序:使用多線程,統計程序源代碼行數;源代碼是能夠編譯經過的合法的代碼, 統計其物理總行數、其中的空行行數、其中含有有效代碼的行數、
* 其中含有註釋內容的行數;(要求必須利用多線程編程, 若是代碼框架能更容易的擴展到支持多種語言的源代碼行數統計,將得到更高的評價。)
*
* @author Administrator
*
*/
public class Work_FourImpl implements Work_Four {
private Work_FourThreadFileImpl filesArray = null;
private Work_FourThreadLineIpml lineArray = null;
private boolean flag = false;
public static void main(String... args) throws Exception {
Work_Four w = new Work_FourImpl();
String path = "D:\\java\\test\\src\\main\\java\\www\\work\\impl";
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Map<String, Long> maps = w.readProject(path);
for (String map : maps.keySet()) {
System.out.println(map + "=" + maps.get(map));
}
}
public Map<String, Long> readProject(String path) throws Exception {
// TODO Auto-generated method stub
exec(path);
while(filesArray.getWf_product().isLineflag()){
}
end();
return Work_Fourutil.getMaps();
}
private void end(){
Work_Fourutil.put("空格行", filesArray.getWf_product().getBlank_Lines());
Work_Fourutil.put("有效代碼行", filesArray.getWf_product().getCode_Lines());
Work_Fourutil.put("註釋行", filesArray.getWf_product().getComment_Lines());
Work_Fourutil.put("物理行", (filesArray.getWf_product().getBlank_Lines()+filesArray.getWf_product().getCode_Lines()+filesArray.getWf_product().getComment_Lines()));
}
private void exec(String path) {
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Work_FourProduct wf_product=new Work_FourProduct(path,".java",wf_file);
//獲取文件目錄的線程
filesArray=new Work_FourThreadFileImpl(wf_product);
Thread thread1 = new Thread(filesArray);
//統計文件的線程
lineArray = new Work_FourThreadLineIpml(wf_product);
Thread thread2 = new Thread(lineArray);
thread1.start();
thread2.start();
}
}
package www.work.service.impl;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import www.work.util.Work_FourArrayFile;
public class Work_FourProduct {
private Work_FourArrayFile wf=null;
private boolean fileflag = true;// 標誌位,判斷得到文件路徑的線程是否運行完成
private boolean lineflag = true;
public boolean isFileflag() {
return fileflag;
}
public void setFileflag(boolean fileflag) {
this.fileflag = fileflag;
}
private String path=null;//要遍歷的文件路徑
private String suffix=null;//文件後綴
private long code_Lines = 0; // 可執行代碼行數
private long comment_Lines = 0; // 註釋行數
private long blank_Lines = 0; // 空格行數
public long getCode_Lines() {
return code_Lines;
}
public long getComment_Lines() {
return comment_Lines;
}
public long getBlank_Lines() {
return blank_Lines;
}
public Work_FourProduct(String path,String suffix,Work_FourArrayFile wf) {
super();
this.path = path;
this.suffix=suffix;
this.wf=wf;
}
public Work_FourArrayFile getWf() {
return wf;
}
public boolean isLineflag() {
return lineflag;
}
public void setLineflag(boolean lineflag) {
this.lineflag = lineflag;
}
public synchronized void execSetWf() throws Exception{
while(true){
if(wf.getIndex()>0){
this.wait();
}else{
break;
}
}
readsFile(path,suffix);
this.notify();
}
private void readsFile(String path,String suffix) throws Exception {
// TODO Auto-generated method stub
File f = new File(path);
File[] ff = f.listFiles();
for (File child : ff) {
if (child.isDirectory()) {
readsFile(child.getPath(),suffix);
} else if (child.getName().matches(".*\\"+suffix+"$")) {
wf.add(child.getPath());
}
}
}
public synchronized void execGetWf() throws InterruptedException{
while(true){
if(wf.getIndex()<=0){
this.wait();
}else{
exec();
this.notify();
break;
}
}
}
private void exec() {
String[] files = wf.getFiles();
if (files != null&&files.length>0) {
for (String f : files) {
try {
readFileLines(new File(f));
wf.remove(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void readFileLines(File f) throws Exception {
BufferedReader br = null;
boolean tempFlag = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim(); // 除去註釋前的空格
if (line.matches("^[ ]*$")) { // 匹配空行
blank_Lines++;
} else if (line.startsWith("//")) {
comment_Lines++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
comment_Lines++;
tempFlag = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
comment_Lines++;
} else if (tempFlag == true) {
comment_Lines++;
if (line.endsWith("*/")) {
tempFlag = false;
}
} else {
code_Lines++;
}
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
throw e;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package www.work.service.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import www.work.util.Work_FourArrayFile;
/**
* 此類用於讀取指定目錄下的java源文件
* @author 陳硯庭
*
*/
public class Work_FourThreadFileImpl implements Runnable {
public static void main(String ...args){
String path="D:\\java\\test\\src\\main\\java\\www\\work\\impl";
Work_FourArrayFile wf_file=new Work_FourArrayFile();
Work_FourProduct wf_product=new Work_FourProduct(path,".java",wf_file);
Work_FourThreadFileImpl w=new Work_FourThreadFileImpl(wf_product);
Thread t=new Thread(w);
t.start();
//System.out.println(Work_FourArrayFile.getFiles());
}
public Work_FourProduct getWf_product() {
return wf_product;
}
private Work_FourProduct wf_product=null;
public Work_FourThreadFileImpl(Work_FourProduct wf_product) {
super();
this.wf_product=wf_product;
}
public void run() {
// TODO Auto-generated method stub
while(wf_product.isFileflag()){
try {
wf_product.execSetWf();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
wf_product.setFileflag(false);
System.out.println("Work_FourThreadFileImpl線程執行完畢!!!!");
}
}
}
}
package www.work.service.impl;
/**
* 此線程類用於保存行數
*
* @author Administrator
*
*/
public class Work_FourThreadLineIpml implements Runnable {
private Work_FourProduct wf_product = null;
public Work_FourThreadLineIpml(Work_FourProduct wf_product) {
this.wf_product = wf_product;
}
public void run() {
// TODO Auto-generated method stub
while (wf_product.isLineflag()) {
try {
wf_product.execGetWf();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
wf_product.setLineflag(false);
System.out.println("Work_FourThreadLineIpml線程執行完畢!!!!");
}
}
}
}
框架
經過此次的面試,明顯的知道了本身的不足,之後要多往算法複雜度方面去思考問題,上面的方法都是能夠改進的。
ide