发表于: 2017-08-15 20:17:31

2 1040


今天:修改了部分接口。


找了一个网站,有一些题目,从最简单的开始做了几个。


1.两个字符串,选出唯一的字符,从小到大排列。感觉自己的方法很笨。例如

"zxcv","zcer"

应该得到cervxz

public class UniqueChar {

    public static void main(String[] args) {
        UniqueChar war = new UniqueChar();
        System.out.println(war.stringCombine("zxcv","zcer"));
    }
    public String stringCombine(String s1, String s2) {
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < s1.length(); i++) {
            set.add(s1.charAt(i));
        }
        for (int i = 0; i < s2.length(); i++) {
            set.add(s2.charAt(i));
        }
        PriorityQueue<Character> queue = new PriorityQueue<>();
        for (Character ch : set) {
            queue.add(ch);
        }
        StringBuilder builder = new StringBuilder();
        while (queue.size() > 0){
            builder.append(queue.poll());
        }
        return builder.toString();
    }
}


2. 给一个数组,里面是东南西北四个方向,每出现一次走一格,怎么判断最后是不是回到原点。这个和标准答案一样。

public class TakeATenMinuteWalk {
    public static boolean isValid(char[] walk){
        int v = 0;
        int h = 0;
        for (char ch : walk){
            if (ch == 'n'){
                v++;
            }
            if (ch == 's'){
                v--;
            }
            if (ch == 'e'){
                h--;
            }
            if (ch == 'w'){
                h++;
            }
        }
        if(v == 0 && h == 0){
            return true;
        }
        return false;
    }
}


3. 给一个数组,把奇数排序,偶数位置保持不变。例如:

sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]


第二个方法是标准答案,用的都是jdk8的东西,看不太懂。

public class Sorttheodd {
    public static void main(String[] args) {
        int [] arr = {2,5,32,1,6,5,8};
        arr = sortArray(arr);
        for (int i : arr){
            System.out.println(i);
        }
    }
    public static int[] sortArray(int[] array) {
        ArrayList<Integer> index = new ArrayList<>();
        ArrayList<Integer> odd = new ArrayList<>();
        for(int i = 0; i < array.length; i++){
            if (array [i] % 2 != 0){
                index.add(i);
                odd.add(array [i]);
            }
        }
        Collections.sort(odd);
        for(int i = 0; i < index.size(); i++){
            array [index.get(i)] = odd.get(i);
        }
        return array;
    }
    public static int[] sortArrayBest(final int[] array) {
        // Sort the odd numbers only
        final int[] sortedOdd = Arrays.stream(array).filter(e -> e % 2 == 1).sorted().toArray();
        // Then merge them back into original array
        for (int j = 0, s = 0; j < array.length; j++) {
            if (array[j] % 2 == 1) array[j] = sortedOdd[s++];
        }
        return array;
    }
}

问题:知识掌握


明天:看情况


总结:无


进度:http://task.ptteng.com/zentao/project-burn-277.html

demo时间:延期


返回列表 返回列表
评论

    分享到