前言:
我堅信跟衅,機會永遠屬于有準備的人,我們與其羨慕他人的成功璃饱,不如從此刻起与斤,積累足夠多的的知識和面試經(jīng)驗,為將來進入更好的工作做好充分的準備荚恶!
算法面試題+答案
1. 統(tǒng)計一篇英文文章單詞個數(shù)撩穿。
public class WordCounting {
public static void main(String[] args) {
try(FileReader fr = new FileReader("a.txt")) {
int counter = 0;
Boolean state = false;
int currentchar;
while((currentchar= fr.read()) != -1) {
if(currentchar== ' ' || currentchar == 'n'|| currentchar == 't' || currentchar == 'r') {
state = false;
} else if(!state) {
state = true;
counter++;
}
}
System.out.println(counter);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
補充:這個程序可能有很多種寫法,這里選擇的是Dennis M. Ritchie和Brian W. Kernighan老師在他們不朽的著作《The C Programming Language》中給出的代碼谒撼,向兩位老師致敬食寡。下面的代碼也是如此。
2. 輸入年月日廓潜,計算該日期是這一年的第幾天抵皱。
public class DayCounting {
public static void main(String[] args) {
int[][] data = {
{31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Scanner sc = new Scanner(System.in);
System.out.print("請輸入年月日(1980 11 28): ");
int year = sc.nextint();
int month = sc.nextint();
int date = sc.nextint();
int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];
int sum = 0;
for (int i = 0; i < month -1; i++) {
sum += daysOfMonth[i];
}
sum += date;
System.out.println(sum);
sc.close();
}
}
3. 回文素數(shù):所謂回文數(shù)就是順著讀和倒著讀一樣的數(shù)(例如:11,121辩蛋,1991…)呻畸,回文素數(shù)就是既是回文數(shù)又是素數(shù)(只能被1和自身整除的數(shù))的數(shù)。編程找出11~9999之間的回文素數(shù)悼院。
public class PalindromicPrimeNumber {
public static void main(String[] args) {
for (int i = 11; i <= 9999; i++) {
if(isPrime(i) && isPalindromic(i)) {
System.out.println(i);
}
}
}
public static Boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
public static Boolean isPalindromic(int n) {
int temp = n;
int sum = 0;
while(temp > 0) {
sum= sum * 10 + temp % 10;
temp/= 10;
}
return sum == n;
}
}
4. 全排列:給出五個數(shù)字12345的所有排列伤为。
public class FullPermutation {
public static void perm(int[] list) {
perm(list,0);
}
private static void perm(int[] list, int k) {
if (k == list.length) {
for (int i = 0; i < list.length; i++) {
System.out.print(list[i]);
}
System.out.println();
} else{
for (int i = k; i < list.length; i++) {
swap(list, k, i);
perm(list, k + 1);
swap(list, k, i);
}
}
}
private static void swap(int[] list, int pos1, int pos2) {
int temp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = temp;
}
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
perm(x);
}
}
5. 對于一個有N個整數(shù)元素的一維數(shù)組,找出它的子數(shù)組(數(shù)組中下標連續(xù)的元素組成的數(shù)組)之和的最大值据途。
下面給出幾個例子(最大子數(shù)組用粗體表示):
- 數(shù)組:{ 1, -2, 3,5, -3, 2 }绞愚,結果是:8
- 數(shù)組:{ 0, -2, 3, 5, -1, 2 },結果是:9
- 數(shù)組:{ -9, -2,-3, -5, -3 }颖医,結果是:-2
可以使用動態(tài)規(guī)劃的思想求解:
public class MaxSum {
private static int max(int x, int y) {
return x > y? x: y;
}
public static int maxSum(int[] array) {
int n = array.length;
int[] start = new int[n];
int[] all = new int[n];
all[n - 1] = start[n - 1] = array[n - 1];
for (int i = n - 2; i >= 0;i--) {
start[i] = max(array[i], array[i] + start[i + 1]);
all[i] = max(start[i], all[i + 1]);
}
return all[0];
}
public static void main(String[] args) {
int[] x1 = { 1, -2, 3, 5,-3, 2 };
int[] x2 = { 0, -2, 3, 5,-1, 2 };
int[] x3 = { -9, -2, -3,-5, -3 };
System.out.println(maxSum(x1));
// 8
System.out.println(maxSum(x2));
// 9
System.out.println(maxSum(x3));
//-2
}
}
6. 用遞歸實現(xiàn)字符串倒轉(zhuǎn)
public class StringReverse {
public static String reverse(String originStr) {
if(originStr == null || originStr.length()== 1) {
return originStr;
}
return reverse(originStr.substring(1))+ originStr.charAt(0);
}
public static void main(String[] args) {
System.out.println(reverse("hello"));
}
}
7. 輸入一個正整數(shù)位衩,將其分解為素數(shù)的乘積。
public class DecomposeInteger {
private static List<Integer> list = new ArrayList<Integer>();
public static void main(String[] args) {
System.out.print("請輸入一個數(shù): ");
Scanner sc = new Scanner(System.in);
int n = sc.nextint();
decomposeNumber(n);
System.out.print(n + " = ");
for (int i = 0; i < list.size() - 1; i++) {
System.out.print(list.get(i) + " * ");
}
System.out.println(list.get(list.size() - 1));
}
public static void decomposeNumber(int n) {
if(isPrime(n)) {
list.add(n);
list.add(1);
} else {
doIt(n, (int)Math.sqrt(n));
}
}
public static void doIt(int n, int div) {
if(isPrime(div) && n % div == 0) {
list.add(div);
decomposeNumber(n / div);
} else {
doIt(n, div - 1);
}
}
public static Boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n);i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
}
8. 一個有n級的臺階熔萧,一次可以走1級糖驴、2級或3級僚祷,問走完n級臺階有多少種走法。
public class GoSteps {
public static int countWays(int n) {
if(n < 0) {
return 0;
} else if(n == 0) {
return 1;
} else {
return countWays(n - 1) + countWays(n - 2) + countWays(n -3);
}
}
public static void main(String[] args) {
System.out.println(countWays(5));
// 13
}
}
9. 寫一個算法判斷一個英文單詞的所有字母是否全都不同(不區(qū)分大小寫)
public class AllNotTheSame {
public static Boolean judge(String str) {
String temp = str.toLowerCase();
int[] letterCounter = new int[26];
for (int i = 0; i <temp.length(); i++) {
int index = temp.charAt(i)- 'a';
letterCounter[index]++;
if(letterCounter[index] > 1) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(judge("hello"));
System.out.print(judge("smile"));
}
}
10. 有一個已經(jīng)排好序的整數(shù)數(shù)組贮缕,其中存在重復元素久妆,請將重復元素刪除掉,例如跷睦,A= [1, 1, 2, 2, 3],處理之后的數(shù)組應當為A= [1, 2, 3]肋演。
public class RemoveDuplication {
public static int[] removeDuplicates(int a[]) {
if(a.length <= 1) {
return a;
}
int index = 0;
for (int i = 1; i < a.length; i++) {
if(a[index] != a[i]) {
a[++index] = a[i];
}
}
int[] b = new int[index + 1];
System.arraycopy(a, 0, b, 0, b.length);
return b;
}
public static void main(String[] args) {
int[] a = {1, 1, 2, 2, 3};
a = removeDuplicates(a);
System.out.println(Arrays.toString(a));
}
}
11. 給一個數(shù)組抑诸,其中有一個重復元素占半數(shù)以上,找出這個元素爹殊。
public class FindMost {
public static <T> T find(T[] x){
T temp = null;
for (int i = 0, nTimes = 0; i< x.length;i++) {
if(nTimes == 0) {
temp= x[i];
nTimes= 1;
} else {
if(x[i].equals(temp)) {
nTimes++;
} else {
nTimes--;
}
}
}
return temp;
}
public static void main(String[] args) {
String[]strs = {"hello","kiss","hello","hello","maybe"};
System.out.println(find(strs));
}
}
12. 編寫一個方法求一個字符串的字節(jié)長度?
public int getWordCount(String s){
int length = 0;
for (int i = 0; i < s.length(); i++)
{
int ascii = Character.codePointAt(s, i);
if(ascii >= 0 && ascii <=255)
length++; else
length += 2;
}
return length;
}
寫在最后:
想進大廠蜕乡,光會算法可不行喲,像Kafka梗夸、Mysql层玲、Tomcat、Docker反症、Spring辛块、MyBatis、Nginx铅碍、Netty润绵、Dubbo、Redis胞谈、Netty尘盼、Spring cloud、分布式烦绳、高并發(fā)卿捎、性能調(diào)優(yōu)、微服務等架構技術都要掌握径密!
針對以上技術點呢午阵,筆者也收集了一些面試題(詳情后面截圖),現(xiàn)在免費分享給大家睹晒!點擊下方傳送門即可免費領取
傳送門
筆者收集的部分面試題截圖