当前位置: 首页 > article >正文

LeetCode之常用函数

 收集了 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);
    }


http://www.kler.cn/a/300534.html

相关文章:

  • CCF刷题计划——解压缩(stoi+bitset双管齐下)
  • git下载安装windows
  • SpringBoot MybatisPlus 打印SQL及参数
  • UnLua调用C++函数
  • 单链表的实现(C语言)
  • MongoDB高可用和分片集群知识
  • 火语言RPA流程组件介绍--鼠标拖拽元素
  • Node.js运行环境搭建
  • 算法题:找出1到1亿中“只有一个重复的”自然数
  • Flask中的钩子函数
  • SpringCloud之配置中心svn示例和refresh
  • go-mciro系列(四)使用nacos作为配置中心_go使用nacos
  • 【无人机设计与控制】固定翼四旋翼无人机UAV俯仰姿态飞行模糊自整定PID控制Simulink建模
  • 大模型的实践应用29-大语言模型的RLHF(人类反馈强化学习)的具体应用与原理介绍
  • 分布式集群下如何做到唯一序列号
  • rhel 8.6 开箱基本设置
  • Python3网络爬虫开发实战(14)资讯类页面智能解析
  • 【大数据算法】一文掌握大数据算法之:空间亚线性算法。
  • windows和linux安装mysql5.7.31保姆级教程
  • C/C++程序的内存开辟