這篇文章主要介紹“Java經典算法題有哪些”,在日常操作中,相信很多人在Java經典算法題有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java經典算法題有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
為密山等地區(qū)用戶提供了全套網頁設計制作服務,及密山網站建設行業(yè)解決方案。主營業(yè)務為網站設計制作、網站設計、密山網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
題目一:
public class testClockwiseOutput { //順時針打印一個矩陣 @Test public void test(){ int[][] num = new int[100][100]; int n = 4; int count =1; for(int i=0;i=end || end<=0)return; for(int i=start;i<=end;i++){ System.out.println(num[start][i]); } for(int i=start+1;i<=end;i++){ System.out.println(num[i][end]); } for(int i=end-1;i>=start;i--){ System.out.println(num[end][i]); } for(int i=end-1;i>start;i--){ System.out.println(num[i][start]); } output(num,start+1,end-1); } }
題目二:
給出一個排序好的數組和一個數,求數組中連續(xù)元素的和等于所給數的子數組
//給出一個排序好的數組和一個數,求數組中連續(xù)元素的和等于所給數的子數組 @Test public void test(){ int[] num = {1,2,2,3,4,5,6,7,8,9}; int sum = 7; findSum(num,sum); } public void findSum(int[] num,int sum){ int left=0; int right=0; for(int i=0;i題目三:
//字符數組組成的所有字符串 @Test public void test(){ //char[] cs = {'a','b','c','d','e'}; char[] cs = {'a','b','c'}; int length = cs.length; recursionSwap(cs,0,length); } public void swap(char[] cs,int index1,int index2){ char temp = cs[index1]; cs[index1]=cs[index2]; cs[index2]=temp; } public void recursionSwap(char[] cs,int start,int length){ if(start>=length-1){ print(cs); return; } for(int i=start;i題目四:
//數組組成的最小數 @Test public void test(){ int[] num={1,5,9,13,442,44,6,21,211}; qsort(num,0,num.length-1); System.out.println(Arrays.toString(num)); } public void qsort(int[] num,int left,int right){ if(lefttemp2){ return true; } else{ return false; } } 題目五:
//子數組***和 @Test public void test(){ int[] num = {1,-2,3,10,-4,7,2,-5}; //int[] num = {1,-2,3,10,-4,10,2,-5}; System.out.println(maxSum(num)); } public int maxSum(int[] num){ int curSum = 0; int curMaxSum = -99999999; int start = 0; int end = 0; for(int i=0;icurMaxSum){ curMaxSum = curSum; end = i; } } for(int i = start;i<=end;i++){ System.out.println(num[i]); } return curMaxSum; } 題目六:
public class testMinStack { //自定義棧,min函數得到當前最小值 @Test public void test(){ MinStack ms = new MinStack(); ms.push(5); System.out.println(ms.min()); ms.push(6); ms.push(2); ms.push(1); System.out.println(ms.min()); ms.pop(); System.out.println(ms.min()); ms.pop(); System.out.println(ms.min()); } } class MinStack{ private StackminStack = new Stack (); private Stack stack = new Stack (); public int pop(){ minStack.pop(); return stack.pop(); } public void push(int num){ if(minStack.size()<=0){ minStack.push(num); return; } Integer min = minStack.lastElement(); if(num 題目七:
//找出數組中出現次數大于一半的數 @Test public void test(){ int[] num = {1,2,2,2,2,2,2,4,2,4,6,4,2,6,8,2,7,7}; System.out.println(moreThanHaft(num)); } public int moreThanHaft(int[] num){ int result = -1; int times = 0; for(int i=0;i題目八:
//判斷一個數組是否是另一個棧的出棧順序 @Test public void test(){ int[] num = {1,2,3,4,5}; //int[] num1={1,2,3,5,4}; int[] num2={2,1,5,3,4}; Stacks1 = new Stack (); Stack s2 = new Stack (); for(int i=0;i<5;i++){ s2.push(num2[i]); } System.out.println(testOrder(num,s1,s2)); } public boolean testOrder(int[] num,Stack s1,Stack s2){ int length = num.length; for(int i=0;i 題目九:
//從撲克牌抽5張牌,0可以為任意數,判斷是否是順子 @Test public void test(){ int[] num = {0,1,5,3,2}; System.out.println(check(num)); } public boolean check(int[] num){ //0-13 int[] pai = new int[14]; for(int n : num){ pai[n]+=1; } qsort(num,0,num.length-1); int count = pai[0]; int start = 0; if(num[0]==0){ start=num[1]; } else{ start=num[0]; } for(int i = start;i<=start+5;i++){ if(pai[i]>1)return false; count += pai[i]; } if(count == 5)return true; else return false; } public void qsort(int[] num,int left,int right){ if(left=partition){ right--; } swap(num,left,right); while(left 題目十:
//輸出第k個丑數(因子只有2,3,5) @Test public void test(){ findUglyNum(8); } public void findUglyNum(int index){ int[] num = new int[index]; int next = 1; num[0]=1; int index2=0; int index3=0; int index5=0; while(next到此,關于“Java經典算法題有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
本文標題:Java經典算法題有哪些
文章出自:http://weahome.cn/article/pgsjed.html