下面是某些企業(yè)常見的算法面試試題,現(xiàn)總結如下,僅供學員參考與學習。
1.比較兩個字符串如果不等返回True?
答案:
Java代碼 
- package com.test.kaoshi;
-
- public class StringDemo {
-
- private static String a = "abc";
- private static String b = "abcg";
-
- public static boolean equalString() {
- if (a.equals(b)) {
- return false;
- } else {
-
- return true;
- }
- }
- public static void main(String[] args) {
- StringDemo sd = new StringDemo();
- System.out.println("主要考察返回Boolean變量和字符串比較使用的方法?+sd.equalString());
- }
- }
- package com.test.kaoshi;
-
- public class StringDemo {
-
- private static String a = "abc";
- private static String b = "abcg";
-
- public static boolean equalString() {
- if (a.equals(b)) {
- return false;
- } else {
-
- return true;
- }
- }
- public static void main(String[] args) {
- StringDemo sd = new StringDemo();
- System.out.println("主要考察返回Boolean變量和字符串比較使用的方法?+sd.equalString());
- }
- }
2.隨機產(chǎn)生20個字符并且排序?
答案:
Java代碼 
- package com.test.kaoshi;
-
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Random;
- import java.util.Set;
- import java.util.TreeSet;
-
- public class RadomDemo {
- /**
- * 隨機產(chǎn)生20個字符串并且字符串不能重復 且進行排序
- * @param random
- * @param len
- * @return
- */
- public Set getChar(){
-
- Set numberSet01 = new HashSet();
- Random rdm = new Random();
- char ch;
- while(numberSet01.size()<20){
- int rdGet = Math.abs(rdm.nextInt())%26+97;//產(chǎn)生97到122的隨機數(shù)a-z值
- ch=(char)rdGet;
- numberSet01.add(ch);
- //Set中是不能放進重復的值的,當它有20個時,就滿足你的條件了
- }
- return numberSet01;
- }
- public static void main(String[] args) {
- RadomDemo rd = new RadomDemo();
- Set numberSet01=rd.getChar();
-
- Set numberSet = new TreeSet();
- numberSet.addAll(numberSet01);
- for(Iterator it=numberSet01.iterator();it.hasNext();){
- System.out.print(it.next());
- }
- System.out.println();
- for(Iterator it=numberSet.iterator();it.hasNext();){
- System.out.print(it.next());
- }
- }
- }
- package com.test.kaoshi;
-
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.Random;
- import java.util.Set;
- import java.util.TreeSet;
-
- public class RadomDemo {
- /**
- * 隨機產(chǎn)生20個字符串并且字符串不能重復 且進行排序
- * @param random
- * @param len
- * @return
- */
- public Set getChar(){
-
- Set numberSet01 = new HashSet();
- Random rdm = new Random();
- char ch;
- while(numberSet01.size()<20){
- int rdGet = Math.abs(rdm.nextInt())%26+97;//產(chǎn)生97到122的隨機數(shù)a-z值
- ch=(char)rdGet;
- numberSet01.add(ch);
- //Set中是不能放進重復的值的,當它有20個時,就滿足你的條件了
- }
- return numberSet01;
- }
- public static void main(String[] args) {
- RadomDemo rd = new RadomDemo();
- Set numberSet01=rd.getChar();
-
- Set numberSet = new TreeSet();
- numberSet.addAll(numberSet01);
- for(Iterator it=numberSet01.iterator();it.hasNext();){
- System.out.print(it.next());
- }
- System.out.println();
- for(Iterator it=numberSet.iterator();it.hasNext();){
- System.out.print(it.next());
- }
- }
- }
3.50個人圍成一圈數(shù)到三和三的倍數(shù)時出圈,問剩下的人是誰?在原來的位置是多少?
答案:
Java代碼 
- package com.test.kaoshi;
-
- import java.util.Iterator;
- import java.util.LinkedList;
-
- public class YouXi {
- public static int removeNM(int n, int m) {
- LinkedList ll = new LinkedList();
- for (int i = 0; i < n; i++)
- ll.add(new Integer(i + 1));
- int removed = -1;
- while (ll.size() > 1) {
- removed = (removed + m) % ll.size();
- ll.remove(removed--);
- }
- return ((Integer) ll.get(0)).intValue();
- }
-
- public static void main(String[] args) {
- System.out.println(removeNM(50, 3));
- }
- }
|