iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >有哪些Java编写的LeetCode题目可以帮助你提高算法能力?
  • 0
分享到

有哪些Java编写的LeetCode题目可以帮助你提高算法能力?

索引npmleetcode 2023-09-23 21:09:20 0人浏览 佚名
摘要

Java是一种广泛使用的编程语言,而LeetCode是一个非常受欢迎的在线编程平台,它提供了大量的算法题目,能够帮助程序员提高算法能力。在这篇文章中,我们将介绍一些Java编写的LeetCode题目,这些题目可以帮助你提高算法能力。 两数

Java是一种广泛使用的编程语言,而LeetCode是一个非常受欢迎的在线编程平台,它提供了大量的算法题目,能够帮助程序员提高算法能力。在这篇文章中,我们将介绍一些Java编写的LeetCode题目,这些题目可以帮助你提高算法能力。

  1. 两数之和

题目描述:

给定一个整数数组nums和一个目标值target,在数组中找出和为目标值的两个整数。

示例:

给定nums = [2, 7, 11, 15],target = 9,因为nums[0] + nums[1] = 2 + 7 = 9,所以返回[0, 1]。

代码演示:

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[] { map.get(complement), i };
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}
  1. 盛最多水的容器

题目描述:

给定一个非负整数数组,每个元素代表一个垂直线的高度,找到两个线段,使得它们与x轴共同构成的容器可以容纳最多的水。

示例:

输入: [1,8,6,2,5,4,8,3,7]

输出: 49

代码演示:

public int maxArea(int[] height) {
    int i = 0, j = height.length - 1;
    int maxArea = 0;
    while (i < j) {
        int area = Math.min(height[i], height[j]) * (j - i);
        maxArea = Math.max(maxArea, area);
        if (height[i] < height[j]) {
            i++;
        } else {
            j--;
        }
    }
    return maxArea;
}
  1. 无重复字符的最长子串

题目描述:

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例:

输入: "abcabcbb"

输出: 3

代码演示:

public int lengthOfLongestSubstring(String s) {
    int n = s.length(), ans = 0;
    Map<Character, Integer> map = new HashMap<>();
    for (int j = 0, i = 0; j < n; j++) {
        if (map.containsKey(s.charAt(j))) {
            i = Math.max(map.get(s.charAt(j)), i);
        }
        ans = Math.max(ans, j - i + 1);
        map.put(s.charAt(j), j + 1);
    }
    return ans;
}
  1. 最长公共前缀

题目描述:

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

示例:

输入: ["flower","flow","flight"]

输出: "fl"

代码演示:

public String longestCommonPrefix(String[] strs) {
    if (strs == null || strs.length == 0) {
        return "";
    }
    String prefix = strs[0];
    for (int i = 1; i < strs.length; i++) {
        while (strs[i].indexOf(prefix) != 0) {
            prefix = prefix.substring(0, prefix.length() - 1);
            if (prefix.isEmpty()) {
                return "";
            }
        }
    }
    return prefix;
}
  1. 三数之和

题目描述:

给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a + b + c = 0?找出所有满足条件且不重复的三元组。

示例:

给定数组nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:

[

[-1, 0, 1],

[-1, -1, 2]

]

代码演示:

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> ans = new ArrayList<>();
    if (nums == null || nums.length < 3) {
        return ans;
    }
    Arrays.sort(nums);
    for (int i = 0; i < nums.length - 2; i++) {
        if (nums[i] > 0) {
            break;
        }
        if (i > 0 && nums[i] == nums[i - 1]) {
            continue;
        }
        int left = i + 1, right = nums.length - 1;
        while (left < right) {
            int sum = nums[i] + nums[left] + nums[right];
            if (sum == 0) {
                ans.add(Arrays.asList(nums[i], nums[left], nums[right]));
                while (left < right && nums[left] == nums[left + 1]) {
                    left++;
                }
                while (left < right && nums[right] == nums[right - 1]) {
                    right--;
                }
                left++;
                right--;
            } else if (sum < 0) {
                left++;
            } else {
                right--;
            }
        }
    }
    return ans;
}

总结

以上就是一些Java编写的LeetCode题目,这些题目可以帮助你提高算法能力。它们涵盖了多个算法问题,包括查找、排序、字符串操作、数组操作等,对于提高算法能力有很大的帮助。如果你想进一步提高自己的算法能力,建议多做LeetCode题目,并结合实际问题练习算法。

--结束END--

本文标题: 有哪些Java编写的LeetCode题目可以帮助你提高算法能力?

本文链接: https://www.lsjlt.com/news/416682.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作