当前位置:   article > 正文

leetcode分类刷题_leetcode 分类题型讲解视频

leetcode 分类题型讲解视频

1. 数组操作

1.1 leetcode27 移除元素

1.2 leetcode283 移动零

个人思想:每检测到0时,与最后元素互换。
参考答案:慢指针存放非0,快指针遍历。

1.3 leetcode485 最大连续1的个数

个人思想:设置一个计数器,当检测到1时,i++. 检测到非1时,计数器值归0.

2. 链表操作

1 创建链表
	LinkedList<Interger> list =  new LinkedList<>();
2 添加元素
	list.add(1) //在末尾插入元素
	list.add(2,99) //在索引为2的位置上插入元素99
3 访问元素
	int element = list.get(2); //访问索引为2 的元素
4 搜索元素
    int index = list.indexOf(99); // 找到元素 返回索引
5 更新元素
    list.set(2,88); // 将索引为2的位置的元素更新为88
6 删除元素
    list.remove(2);
7 长度
    int length = list.size();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.1 leetcode203 移除链表元素

2.2 leetcode206 反转链表

dummy = ListNode(0); //设置头结点
dumy.next = head;
while(head != null && head.next != null){
设置两个保存指针
}
return dummy.next;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. Java队列Queue

1 创建队列
	Queue<Integer> queue = new LinkedList<>();
2 添加元素
	queue.add(1);
3 获取即将出队的元素
	int temp = queue.peek();  // 不常见peek()函数
4 删除即将出队的元素
   int temp = queue.poll();
5 判断队列是否为空
	queue.isEmpty(); //返回 1 or 0
6 队列的长度
	int i = queue.size();
7 遍历队列
 	 while(!queue.isEmpty){
 	 System.out.println(queue.poll());
 	 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3.1 leetcode933 最近的请求次数 (仅一道题目)

class RecentCounter {
    Queue<Integer> q;
    public RecentCounter() {
        q = new LinkedList(); //构造函数就是初始化这个队列Q
    }
    public int ping(int t) {
        q.add(t);
        while (q.peek() < t - 3000)
            q.poll();
        return q.size();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.2 leetcode239 滑动窗口最大值 (困难)

4. Java栈的操作

1 创建栈Stack
	Stack<Integer> stack = new LinkedList<>();
2 添加元素
	stack.add(1);
3 获取栈顶元素
	stack.peek();
4 删除栈顶元素
	stack.pop();
5 判断栈是否为空
	stack.isEmpty();
6 栈的遍历
	while(!stack.isEmpty()){
	int num = stack.pop();
	//输出num;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4.1 leetcode20 有效的括号(easy)

class Solution {
    public boolean isValid(String s) {
        if(s.length()%2==0){
            Stack<Character> stack=new Stack<>();
            for(int i=0;i<s.length();i++){
                char c=s.charAt(i);
                if(c=='('||c=='['||c=='{'){
                    stack.push(c);
                }
                else if(c==')'){
                    if(!stack.isEmpty()&&stack.peek()=='('){stack.pop();}
                    else{return false;}
                }
                else if(c=='}'){
                    if(!stack.isEmpty()&&stack.peek()=='{'){stack.pop();}
                    else{return false;}
                }
                else{
                    if(!stack.isEmpty()&&stack.peek()=='['){stack.pop();}
                    else{return false;}
                }
            }
            return stack.isEmpty();
        }
        return false;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

4.2 leetcode496 下一个更大元素 I

两个栈实现,删除一个栈的元素 存在另一个临时栈中

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        Stack<Integer> stack1 = new Stack<Integer>();
        for(int i = 0; i < nums2.length; i++){
            stack1.push(nums2[i]);
        }
        for(int i = 0; i < nums1.length; i++){
            boolean isFound = false;
            int max = -1;
            Stack<Integer> stack2 = new Stack<>();
            while(!stack1.isEmpty()&&!isFound){
                int temp = stack1.pop();
                if(nums1[i] < temp){
                    max = temp;
                }else if(nums1[i] == temp){
                    isFound = true;
                }
                stack2.push(temp);
            }
            res[i] = max;
            while(!stack2.isEmpty()){
                stack1.push(stack2.pop());
            }
        }
        return res;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

5. Hash Table 哈希表

哈希表==散列表 键值对 在java中是HashMap结构

1 创建哈希表
	HashMap<Integer, String> map = new HashMap<>();
2 添加元素
	map.put(1,"suncunwei");
3 更新元素
	map.put(1,"zhonghailing");
4 删除元素
	map.remove(1);
5 获取元素
	map.get(1) //1表示键 获取的是value
6 查询key存在?
 	map.containsKey(3);
7 长度
	map.size();
8 是否含元素
	map.isEmpty();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

5.1 leetcode217 存在重复元素

key: 是数组元素,value: 元素出现次数。 HashMap

5.2 leetcode389 找不同

class Solution {
    public char findTheDifference(String s, String t) {
        int[] cnt = new int[26];
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            cnt[ch - 'a']++;
        }
        for (int i = 0; i < t.length(); ++i) {
            char ch = t.charAt(i);
            cnt[ch - 'a']--;
            if (cnt[ch - 'a'] < 0) {
                return ch;
            }
        }
        return ' ';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

5.3 leetcode496 下一个更大元素 I

class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        Stack<Integer> stack = new Stack<>();
        HashMap<Integer,Integer> map = new HashMap<>();

        for(int i = 0; i < nums2.length; i++){
            while(stack.size() != 0 && nums2[i] > stack.peek()){
                int temp = stack.pop();
                map.put(temp,nums2[i]);
            }
            stack.push(nums2[i]);
        }
        while(stack.size() != 0){
            int temp = stack.pop();
            map.put(temp,-1);
        }
        for(int j = 0; j < nums1.length; j++){
            res[j] = map.get(nums1[j]);
        }
        return res;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

6. 集合Set在Java中的常用操作

1 创建集合
	HashSet<Integer> set = new HashSet<>();
2 添加元素
	set.add(10);
3 搜索元素
    set.contains(10);
4 删除元素
	set.remove(10);
5 长度
	set.size();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6.1 leetcode217 存在重复元素

比较长度: 集合和数组元素长度一样 则不存在重复元素。

6.2 leetcode705 设计哈希集合

7. 待更新

8. 待更新

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/512157
推荐阅读
相关标签
  

闽ICP备14008679号