面试中的算法(持续更新中)

  • 给定一个数组a,长度为n,求数组中所有两个元素相加之和为N的小标i和j

  • import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int N = sc.nextInt();
            int[] arr = new int[n];
            Map map = new HashMap<>();
            for (int i=0;i
  • 三个线程的名字分别为0,1,2,每个线程输出十次,按照012012012. . .的方式输出

  • public class Main {
        //用来记录前一个数的值
        int x=-1;
    
        public static void main(String[] args){
            Main m = new Main();
    
            new Thread(m.new RunnableA("0")).start();
            new Thread(m.new RunnableB("1")).start();
            new Thread(m.new RunnableC("2")).start();
        }
    
        class RunnableA implements Runnable{
            private String name;
    
            public RunnableA(String name) {
                this.name = name;
            }
    
            @Override
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (Main.class){
                        if(x == 1 || x == 0){
                            try {
                                Main.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            i--;
                            continue;
                        } else {
                            System.out.print(name);
                            x = 0;
                            Main.class.notifyAll();
                        }
                    }
                }
            }
        }
    
        class RunnableB implements Runnable{
            private String name;
    
            public RunnableB(String name){
                this.name = name;
            }
    
            @Override
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (Main.class){
                        if(x == 1 || x == 2){
                            try {
                                Main.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            i--;
                            continue;
                        } else {
                            System.out.print(name);
                            x = 1;
                            Main.class.notifyAll();
                        }
                    }
                }
            }
        }
    
        class RunnableC implements Runnable{
            private String name;
    
            public RunnableC(String name){
                this.name = name;
            }
    
            @Override
            public void run(){
                for(int i=0;i<10;i++){
                    synchronized (Main.class){
                        if(x == 0 || x == 2){
                            try {
                                Main.class.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            i--;
                            continue;
                        } else {
                            System.out.print(name);
                            x = 2;
                            Main.class.notifyAll();
                        }
                    }
                }
            }
    
        }
    }
  • 给定一个字符串,得到这个数组按顺序最小的字符串输出,最小的输出个数为s

  • 例如:str=1584632   s=3   输出:132
  • public class Main {
        public static void main(String[] args) {
            Scanner sc1 = new Scanner(System.in);
            Scanner sc2 = new Scanner(System.in);
    
            //输入一个字符串
            String str = sc1.nextLine();
            //得到剩余的最小的个数
            int s = sc2.nextInt();
    
            //将字符串变成 int[]
            int[] arr = new int[str.length()];
            for (int i=0;i map = new HashMap<>();
            List list = new ArrayList<>();
            for (int i=0;i map1 = new HashMap<>();
            for (int i=0;i> list1 = new ArrayList<>();
            Set> entries = map1.entrySet();
            for (Map.Entry e : entries){
                list1.add(e);
            }
            Collections.sort(list1, new Comparator>() {
                @Override
                public int compare(Map.Entry o1, Map.Entry o2) {
                    return o1.getValue() - o2.getValue();
                }
            });
    
            //排序完成后进行输出,如果首位为 0 那么跳过
            for (Map.Entry m : list1){
                if (m.getKey() != 0){
                    System.out.print(m.getKey());
                }
            }
        }
    }
  • 比较版本号:第二个版本号大于第一个那么就返回true,否则返回false

  • import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
    
            for (int q = 0;qarr2[i]){
                            System.out.println("false");
                            break;
                        }
                    }
                }
            }
        }
    }
  • 一个数的各个位的平方和得到一个数,如果得到的结果为1,则返回true,否则继续这行这个操作

  • 例如:19
  • 1^2+9^2=82
  • 8^2+2^2=68
  • 6^2+8^2=100
  • 1^2+0^2+0^2=1
  • 则返回true
  • import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for (int i = 0; i < n; i++) {
                int t = sc.nextInt();
                slove(t);
            }
            sc.close();
        }
    
        public static void slove(int t){
    
            Map map = new HashMap<>();
            map.put(t," ");
            int num = 0;
            int k = 0;
            while (t!=1){
                char[] arr = String.valueOf(t).toCharArray();
                num = 0;
                for (int j = 0; j < arr.length; j++) {
                    k = arr[j] - '0';
                    num+=k*k;
                }
                t = num;
                //如果存在当前key那么就不会得到1,输出false
                if(map.get(t)!=null){
                    System.out.println("false");
                    return;
                }else {
                    map.put(t," ");
                }
            }
            System.out.println("true");
        }
    }
    
  • 数字和字符串进行归并,每四个数字后边跟一个字母,如果字母多余那么跟在后边
  • import java.util.*;
    
    public class text {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String s1 = scanner.nextLine();
            String s2 = scanner.nextLine();
            String[] split1 = s1.split(" ");
            String[] split2 = s2.split(" ");
            if (split1.length == 0 && split2.length == 0) {
                System.out.println();
                return;
            }
            if (split1.length==0){
                System.out.println(s2);
                return;
            }
            if (split2.length==0){
                System.out.println(s1);
                return;
            }
    
            StringBuffer sb = new StringBuffer();
            int index = 0;
            for (int i=0;i
  • 面试中的算法(持续更新中)_第1张图片
  • 面试中的算法(持续更新中)_第2张图片
  • import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String s = sc.nextLine();
            //\\s表示   空格,回车,换行等空白符,
            //+号表示一个或多个的意思
            String[] str = s.split(",\\s+");
            Map map = new HashMap<>();
            int sum = 0;
            for (int i = 0; i < str.length; i++) {
                int value = Integer.valueOf(str[i]);
                if (map.containsKey(-value) && map.get(-value) > 0) {
                    int count = map.get(-value);
                    map.put(-value, count - 1);
                    sum++;
                } else {
                    int count = map.getOrDefault(value, 0);
                    map.put(value, count + 1);
                }
            }
            System.out.println(sum);
         }
    }
    
  • 实现一个数组的最长递增序列
  • import java.util.*;
    
    public class text5 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String[] str= sc.nextLine().split(" ");
            int[] arr = new int[str.length];
            for (int i = 0; i < str.length; i++){
                arr[i] = Integer.parseInt(str[i]);
            }
            int max = 0, count = 0, ss = 0, x = 0, y = 0;
            for (int i = 0; i < str.length - 1; i++) {
                y = i + 1;//判断是否递增,是的话count++;
                if (arr[i + 1] > arr[i]) {
                    count++;
                    if (count > max) {
                        max = count;
                        ss = x;
                    }
                } else {
                    count = 0;
                    x = y;//不连续递增,则索引改变为下一个目标
                }
            }
            for (int i = ss; i <= ss + max; i++) {
                if (i == (ss + max)) {
                    System.out.print(arr[i]);
                } else {
                    System.out.print(arr[i] + " ");
                }
            }
        }
    }
    
  • 归并排序
  • 
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] a = new int[n];
            for (int i=0;i
  • 分饼干问题:没个人要达到满意,每个人至少分得一个饼干
  • import java.util.*;
    
    public class Main {
        public static void main(String [] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[] arr = new int[n];
            for (int i=0;i arr[i-1]){
                    sum[i] = sum[i-1]+1;
                }
            }
            for(int i = sum.length-2;i>=0;i--){
                if(arr[i] > arr[i+1]){
                    sum[i] = Math.max(sum[i],sum[i+1]+1);
                }
            }
            int num = 0;
            for(int i = 0;i < arr.length;i++){
                num += sum[i];
            }
            return num;
        }
    }
    
  •  

你可能感兴趣的:(java,面试中的算法)