LeetCode 解题记录

LeetCode 解题记录


前言: 这是一篇关于 LeetCode 题解 的文章,总共分为三章:简单题型中等题型困难题型 , 主要用于记录自己的解题过程,同时也为了以后看到类似的问题时,自己能够及时查阅并解决问题。最后,也希望能够帮助到其他人,这才是写这篇博客的初衷。当然了,这里 只提供思路和 Java 语言的实现 ,不提供其他语言的实现。(因为其他语言我不会呀,呜呜呜~ ~ ~)

简单题型

两数之和

题目描述

代码实现

第一种:代码实现简单版,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int[] twoSum(int[] nums, int target) {

for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {

if (nums[i] == target - nums[j]) {
return new int[]{i, j};
}

}
}

throw new IllegalArgumentException("No two sum solution !!!");

}
}

第二种:代码实现高级版,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public static 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
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int removeElement(int[] nums, int val) {
int count = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != val){
nums[count++] = nums[i];
}
}

return count--;
}
}

中等题型

困难题型

  

📚 本站推荐文章
  👉 从 0 开始搭建 Hexo 博客
  👉 计算机网络入门教程
  👉 数据结构入门
  👉 算法入门
  👉 IDEA 入门教程

可在评论区留言哦

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×