Share some typical algorithm questions during the Java interview

Source: Internet
Author: User

1. Write a method and print the 9-9 multiplication table with a for loop.

 

 /** <Br/> * print the jiujiu multiplication table <br/> */<br/> Public void nineninemulittable () {<br/> for (INT I = 1, j = 1; j <= 9; I ++) {<br/> system. out. print (I + "*" + J + "=" + I * j + ""); <br/> if (I = J) {<br/> I = 0; <br/> J ++; <br/> system. out. println (); <br/>}< br/>

 

2. How to convert a java. util. date object to a string in the format of "20:23:22"<Br/>/** <br/> * convert a date to a string in a fixed format <br/> * @ Param date <br/> * @ return STR <br/> */<br/> Public String date2formatstr (date) <br/>{< br/> simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); <br/> string STR = SDF. format (date); <br/> return STR; <br/>}

 

3. Write a method to determine whether any integer is a prime number.

/** <Br/> * determine whether any integer has a prime number <br/> * @ Param num <br/> * @ return Boolean <br/> */<br/> public Boolean isprimenumber (INT num) <br/>{< br/> for (INT I = 2; I <= math. SQRT (Num); I ++) {<br/> If (Num % I = 0) <br/>{< br/> return false; <br/>}< br/> return true; <br/>}< br/>

 

4. Write a method, input any integer, and return its factorial.
<Br/>/** <br/> * obtain the factorial of any integer <br/> * @ Param n <br/> * @ returnn! <Br/> */<br/> Public int factorial (INT num) <br/> {<br/> // recursion <br/> If (num = 1) <br/>{< br/> return 1; <br/>}< br/> return num * factorial (num-1); <br/>}

 

5. Write a method and use the binary search method to determine whether any integer exists in any integer array. If yes, return the index position in the array. If no integer exists, return-1.

<Br/>/** <br/> * binary search for the position of a specific integer in an integer array (recursion) <br/> * @ Param dataset <br/> * @ Param data <br/> * @ Param beginindex <br/> * @ Param endindex <br/> * @ Return Index <br/> */<br/> Public int binarysearch (INT [] dataset, int data, int beginindex, int endindex) {<br/> int midindex = (beginindex + endindex)/2; <br/> // if the number of queries is smaller than that of the Data starting the index or greater than that of the ending index, or if (Data <dataset [beginindex] | DATA> dataset [endindex] | beginindex> endindex) {<br/> return-1; <br/>}< br/> If (Data <dataset [midindex]) {<br/> return binarysearch (dataset, data, beginindex, midIndex-1); <br/>}else if (data> dataset [midindex]) <br/>{< br/> return binarysearch (dataset, Data, midindex + 1, endindex); <br/>}else {<br/> return midindex; <br/>}</P> <p>/** <br/> * binary search for the position of a specific integer in an integer array (non-recursive) <br/> * @ Param dataset <br/> * @ Param data <br/> * @ return index <br/> */<br/> Public int binarysearch (INT [] dataset, int data) <br/>{< br/> int beginindex = 0; <br/> int endindex = dataset. length-1; <br/> int midindex =-1; <br/> If (Data <dataset [beginindex] | DATA> dataset [endindex] | beginindex> endindex) {<br/> return-1; <br/>}< br/> while (beginindex <= endindex) {<br/> midindex = (beginindex + endindex)/2; <br/> If (Data <dataset [midindex]) {<br/> endindex = midIndex-1; <br/>} else if (data> dataset [midindex]) {<br/> beginindex = midindex + 1; <br/>} else {<br/> return midindex; <br/>}< br/> return-1; <br/>}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.