收集了 LeetCode 面试经典150 & LeetCode 热题100中常用的函数与技巧

 LeetCode之数组/字符串

地址链接

    public static void main(String[] args) {
        // 1. 数组排序
        int[] array = new int[]{3, 7, 5, 2};
        Arrays.sort(array);
        // 结果: [2, 3, 5, 7]
        System.out.println(Arrays.toString(array));

        // 2. 数组拷贝
        int[] arrayOld = new int[]{1, 2, 3, 4, 5};
        int[] arrayNew = new int[5];
        System.arraycopy(arrayOld, 0, arrayNew, 0, arrayOld.length - 1);
        // 结果: [1, 2, 3, 4, 5]
        System.out.println(Arrays.toString(arrayOld));
        // 结果: [1, 2, 3, 4, 0]
        System.out.println(Arrays.toString(arrayNew));

        // 3. 最大值最小值
        int max = Math.max(2, 3);
        int min = Math.min(2, 3);
        // 结果 3-2
        System.out.println(max + "-" + min);

        // 4. 随机数生成
        Random random = new Random();
        int randomInt = random.nextInt(10);
        // 结果: 8
        System.out.println(randomInt);

        // 5. HashMap
        Map<String, String> hashMap = Maps.newHashMap();
        hashMap.put("1", "1");
        hashMap.put("2", "2");
        hashMap.put("3", "3");
        // 结果: true
        System.out.println(hashMap.containsKey("1"));

        // 6. ArrayList
        List<String> arrayList = Lists.newArrayList("1", "2", "3");
        arrayList.set(2, "4");
        arrayList.remove(1);
        // 结果: [1, 4]
        System.out.println(arrayList);

        // 7. 数组创建
        int[] arr1 = new int[3];
        arr1[0] = 1;
        int[] arr2 = {1, 2, 3};
        int[] arr3 = new int[]{1,2,3};
        // 结果: [1, 0, 0]
        System.out.println(Arrays.toString(arr1));
        // 结果: [1, 2, 3]
        System.out.println(Arrays.toString(arr2));
        // 结果: [1, 2, 3]
        System.out.println(Arrays.toString(arr3));

        // 8. 字符串
        String str = "hello world";
        char[] charArray = str.toCharArray();
        // 结果: l
        System.out.println(str.charAt(3));
        // 结果: [h, e, l, l, o,  , w, o, r, l, d]
        System.out.println(Arrays.toString(charArray));

        // 9. switch语句
        String condition = "a";
        // 结果: a
        switch (condition) {
            case "a":
                System.out.println("a");
                break;
            case "b":
                System.out.println("b");
                break;
            default:
                System.out.println("c");
        }

        // 10. 可变字符串
        StringBuilder sb = new StringBuilder();
        sb.append("1");
        sb.append("2");
        sb.append("3");
        // 结果: 123
        System.out.println(sb.toString());

        // 11. 字符串去空格
        String strTrim = " tes t ";
        String trim = strTrim.trim();
        // 结果: tes t
        System.out.println(trim);
        // 结果: 5
        System.out.println(strTrim.lastIndexOf("t"));

        // 12. 字符串截取
        String strSub = "test subString";
        String substring = strSub.substring(5, 8);
        System.out.println(substring);

        // 13. 正则&字符串拼接
        String string = " hello world ";
        String trim1 = string.trim();
        // 结果: hello world
        System.out.println(trim1);
        String[] split = trim1.split("\\s+");
        List<String> strings = Arrays.asList(split);
        // 结果: [hello, world]
        System.out.println(strings);
        Collections.reverse(strings);
        // 结果: [world, hello]
        System.out.println(strings);
        // 结果: world,hello
        String join = String.join(",", strings);
        System.out.println(join);
    }

LeetCode之双指针

地址链接

    public static void main(String[] args) {
        // 1. Char操作
        char a = '1';
        char b = 'b';
        // 判断是否字符或数组
        // 结果: true
        System.out.println(Character.isLetter(b));
        // 结果: true
        System.out.println(Character.isDigit(a));
        // 结果: true
        System.out.println(Character.isLetterOrDigit(a));
        char c = 'C';
        // 结果: true
        System.out.println(Character.toLowerCase(c));

        // 2. 可变字符串反转
        StringBuilder sb = new StringBuilder();
        sb.append("1");
        sb.append("2");
        sb.append("3");
        // 结果: 321
        System.out.println(sb.reverse());
    }

LeetCode之滑动窗口

地址链接

    public static void main(String[] args) {
        // 1. Integer最大最小值
        int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;
        // 结果: -2147483648
        System.out.println(min);
        // 结果: 2147483648
        System.out.println(max);

        // 2. Set操作
        Set<String> set = Sets.newHashSet("1", "2", "2", "3");
        // 结果: [1, 2, 3]
        System.out.println(set);
        // 结果: true
        System.out.println(set.contains("2"));
        // 结果: true
        System.out.println(set.remove("3"));
        // 结果: [1, 2]
        System.out.println(set);
    }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部