Java interview-Classic algorithm questions

Source: Internet
Author: User

Topic One:

public class Testclockwiseoutput {//print a matrix clockwise  @Test public void Test () {int[][] num = new INT[100][100]; int n = 4; i NT count = 1; for (int i=0;i<n;i++) {for (int J =0;j<n;j++) {                num[i][j]=count++;            }        }                Output (num,0,n-1);    } public void output (int[][] num,int Start,int end) {if (Start>=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);}    }
Topic Two:

A sorted array and a number are given, and the sum of successive elements in the array is equal to the given number of sub-arrays.

A sorted array and a number are given, and the sum of contiguous elements in the array is equal to the number of substrings  @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<num.length;i++) {int cursum = 0;            left = i;            right = i; while (cursum<sum) {                cursum + = num[right++];            } if (Cursum==sum) {for (int j=left;j<right;j++) {                    System.out.print (num[j]+ "");                }                System.out.println ();}}}    
Topic Three:

The character array consists of all the strings  @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<length;i++) {            swap (cs,start,i);            Recursionswap (cs,start+1,length);                Swap (cs,start,i);        }    } public void print (char[] cs) {for (int i=0;i<cs.length;i++) {            System.out.print (cs[i]);        }        System.out.println ();    }
Topic Four:

The minimum number of array composition @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 (left<right) {int partition = partition (Num,left,right);            Qsort (num,left,partition-1);        Qsort (Num,partition+1,right); }} public int partition (int[] Num,int left,int right) {int partition = Num[left], while (Left<right) {while (num [Right]==partition | |            Ismbigerthann (num,num[right],partition) && left<right) {right--; } swap (Num,left,right);            while ((Num[left]==partition | | Ismbigerthann (num,partition,num[left)) && left<right) {left++;                } swap (Num,left,right);    } return left;        } public void Swap (int[] num,int m,int n) {int temp = num[m];        Num[m]=num[n];    Num[n]=temp; } public boolean Ismbigerthann (int[] Num,int m,int N) {String num1 = string.valueof (m); String num2 = string.valueof (n); int temp1 = Integer.parseint (num1+num2); int temp2 = Integer.parseint (NUM2+NUM1);        if (TEMP1&GT;TEMP2) {return true;        } else{return false; }    }
Topic Five:

Sub-array Max and  @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;i<num. length;i++) {if (cursum<=0) {                cursum = num[i];                start = i;            } else{                cursum + = Num[i];            } if (cursum>curmaxsum) {                curmaxsum = cursum;                        end = i;            }        } for (int i = start;i<=end;i++) {            System.out.println (num[i]);        } return curmaxsum;    }
Topic Six:

 public class Testminstack {//custom stack, min function gets the current minimum @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 stack<integer> Minstack = new stack<integer> (); private stack<integer> Stack = new Stack<integer> ();    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<min) {minstack.push (num);        } else{Minstack.push (min);    } stack.push (num);        } public int min () {if (Minstack.size () <=0) {return-1;    } return Minstack.lastelement (); }}
Topic Seven:

Find out how many occurrences in the array are greater than half  @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<num.length;i++) {if (times==0) {                re Sult = Num[i];                times++;            } else{if (num[i]==result) {                    times++;                } else{                    times--;}}        } return result;    
Topic Eight:

Determines whether an array is the stacking order of another stack  @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};        stack<integer> S1 = new stack<integer> ();        stack<integer> s2 = new stack<integer> (); for (int i=0;i<5;i++) {            s2.push (num2[i]);        }                System.out.println (Testorder (NUM,S1,S2));    } public boolean Testorder (int[] num,stack<integer> s1,stack<integer> s2) {int length = num.length, for (int i=0 ; i<length;i++) {            s1.push (num[i]); int s2num = S2.lastelement (); if (S2num==s1.lastelement (). Intvalue ()) {                S1.pop ();                S2.pop ();            }        } while (!s1.isempty ()) {if (!s1.pop (). Equals (S2.pop ())) {return false;            }        } return true;    }
Topic Nine:

Draw 5 cards from poker, 0 can be any number, determine whether the CIS @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<right) {int partition = partition (Num,left,right);            Qsort (num,left,partition-1);        Qsort (Num,partition+1,right); }} public int partition (int[] Num,int left,int right) {int partition = Num[left], while (Left<right) {while (left<            Right && num[right]>=partition) {right--; } swap (Num,left,right); while (lEft<right && num[left]<=partition) {left++;        } swap (Num,left,right);            } return left;        } public void Swap (int[] num,int m,int n) {int temp = num[m];        Num[m]=num[n];    Num[n]=temp; }
Topic Ten:

Output K-Ugly (factor only 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<index) {int num2 = num[index2]*2; int num3 = num[index3]*3; int num5 = num[index5]*5;                        Num[next] = getsuitable (NUM2,NUM3,NUM5); while (Num[index2]*2<=num[next]) {                index2++,            } while (Num[index3]*3<=num[next]) {                index3++;            } while (Num[index5]*5<=num[next]) {                index5++;            }                            next++;                    }        System.out.println (Num[index-1]);    } public int getsuitable (int num2,int num3,int NUM5) {int s = num2; if (num3<s) {            s = num3;        } if (num5<s) {
   
    s = NUM5;        } return s;    }
   

Java interview-Classic algorithm questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.