java算法面试题
来源:互联网 发布:淘宝收藏怎么看不到 编辑:程序博客网 时间:2024/05/17 01:29
版权声明:本文为博主原创文章,未经博主允许不得转载。
下面是某些企业常见的算法面试试题,现总结如下,仅供学员参考与学习。
1.比较两个字符串如果不等返回True?
答案:
- 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.随机产生20个字符并且排序?
答案:
- 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 {
- /**
- * 随机产生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;//产生97到122的随机数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 {
- /**
- * 随机产生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;//产生97到122的随机数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个人围成一圈数到三和三的倍数时出圈,问剩下的人是谁?在原来的位置是多少?
答案:
- 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));
- }
- }
0 0
- Java算法面试题
- Java算法面试题
- java算法面试题
- Java算法面试题
- java 面试题算法
- Java算法面试题
- java算法面试题
- java算法面试题
- java 面试题和算法
- java算法面试题-----1
- java面试题---算法2
- JAVA-简单面试题-算法
- JAVA面试题(算法篇)
- java算法面试题积累
- java算法面试题之一
- java经典算法面试题(1)
- 三道Java算法面试题
- Java基础面试题3(排序算法)
- 树莓派smplayer无法播放mp3问题
- React Native For Android初体验
- 维基利亚密码
- [转]招聘一个靠谱的iOS面试题
- sql 面试题 函数 求工资第n多的工资
- java算法面试题
- FileOutputStream字节输出流
- 生成式对抗网络GAN研究进展(四)——Laplacian Pyramid of Adversarial Networks,LAPGAN
- IOException的处理
- tcp模拟浏览器向 Tomcat服务器发送请求
- AsyncTask基础<笔记>
- SQL 101 新手常见难点&错误
- 用JAVA实现的DES加密解密算法
- 构造函数语义学---default constructor