美团笔试编程题

美团笔试两道编程题,题目其实不难,没有涉及到很复杂的算法,主要是一个逻辑问题,还是由于自己编程能力弱,没有在有限的时间内解决,还要继续刷题。

自己的方法也不是最优化,具体的题目不记得了,记下,方便以后来看

第一题:求文本距离

import java.util.Scanner;

/**
 * Created by HP on 2018/3/23.
 */
public class Main2 {
    private static int distance(String s1,String s2){
        int d = 0;
        for (int i =0 ;i< s1.length();i++){
            if (s1.charAt(i) != s2.charAt(i)){
                d = d + 1;
            }
        }

        return d;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        String temp ="";
        if (s1.length() >s2.length()){
            temp = s1;
            s1 = s2;
            s2 =temp;
        }
        int d = s1.length();
        for (int i = 0;i

第二题:求不会组成的最小正整数

import java.util.*;

/**
 * Created by HP on 2018/3/22.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s ="";
        Object  num ;
        Set set = new TreeSet<>();
        List list = new ArrayList(){{
            add('0');
            add('1');
            add('2');
            add('3');
            add('4');
            add('5');
            add('6');
            add('7');
            add('8');
            add('9');
        }};
        s = sc.next();
        char[] chars = s.toCharArray();
        for (char c :chars){
            set.add(c);
        }

        for (Object c:set){
            list.remove(c);
        }
        //判断
        if (list.contains('0')){
            Object c = '0';
            list.remove(c);
        }
        if (list.contains('1')){
            System.out.println(1);
            return;

        }else{
            if(list.size() ==0){
                System.out.println(10);
                return;
            }
        }

        num = (list.get(0));
        System.out.println(num);

    }
}

    优化第二道题,思考了一下,这样也是可以的

import java.util.*;

/**
 * Created by HP on 2018/3/22.
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s ="";
        Object  num ;
        List list = new ArrayList(){{
            add('1');
            add('2');
            add('3');
            add('4');
            add('5');
            add('6');
            add('7');
            add('8');
            add('9');
        }};
        s = sc.next();
        char[] chars = s.toCharArray();

        for (Object c:chars){
            list.remove(c);
        }
        //判断
        if (list.contains('1')){
            System.out.println(1);
            return;

        }else{
            if(list.size() ==0){
                System.out.println(10);
                return;
            }
        }

        num = (list.get(0));
        System.out.println(num);

    }
}

你可能感兴趣的:(java)