Leetcode - Top 100 Easy Questions

1. The sum of two numbers

1. The first thought of this topic is to use brute force to crack

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        // cycle
        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return result;

    }
}

2.HashMap method (the idea is to first traverse the array and store it in the Map, and then remove its own data each time it traverses the query. The point to note here is that map.remove(key) is deleted according to the key, map.remover(key,value)) is to delete according to the key-value pair. There may be data such as {3,3}, so if you delete it, use map.remove(key,value), otherwise, if you use map.remove(key), two values ​​will be deleted.

/*Use storage space  */
import java.util.Map;
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        
        // result
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            map.put(nums[i],i);
        }

        for(int i=0;i<nums.length;i++){
            map.remove(nums[i],i);

            if(map.containsKey(target-nums[i])){
                return new int[]{i,map.get(target-nums[i])};
            }
            map.put(nums[i],i);

        }

        return result;

    }
}

3.HashMap optimization method (Letcode official solution, first query and then put into Map)

/*Use storage space  */
import java.util.Map;
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        
        // result
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                return new int[]{i,map.get(target-nums[i])};
            }
            map.put(nums[i],i);

        }

        return result;

    }
}

20. Valid parentheses

  • The core idea of ​​this question is to sort out the logic clearly

Due to the particularity of the stack structure, it is very suitable for symmetric matching problems.
The first thing to make clear is that there are several situations in which parentheses in strings do not match.
Some classmates started to write code when they saw this kind of topic in the interview, and then they wrote more and more chaotically.
It is recommended to analyze the mismatches before writing the code. If you don't analyze it before you start, the code you write will have many problems.
Let's first analyze the three mismatch situations here.
In the first case, the parentheses in the left direction in the string are redundant, so they do not match. !Brackets match 1
In the second case, the parentheses are not redundant, but the type of the parentheses does not match. !Brackets match 2
In the third case, the parentheses in the right direction in the string are redundant, so they do not match. !Brackets match 3
As long as our code covers these three mismatches, there will be no problems. We can see the importance of analyzing the problem before starting.

The animation is as follows:

1. General logic

import java.util.Stack;
class Solution {
    public boolean isValid(String s) {

        // First split the string 
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();

        for(int i=0;i<chars.length;i++){
            if(chars[i]=='('||chars[i]=='{'||chars[i]=='['){
                 stack.push(chars[i]);
            }else{
                if(stack.isEmpty()||(!isMatch(stack.peek(),chars[i]))){
                    return false;
                }
                stack.pop();
            }
        }

        if(stack.isEmpty()){
            return true;
        }else{
            return false;
        }

    }


    public boolean isMatch(char left,char right){
        if(left=='('&&right==')'){return true;}
        if(left=='{'&&right=='}'){return true;}
        if(left=='['&&right==']'){return true;}
        return false;
    }
}

2.Letcode classic logic

import java.util.Stack;
class Solution {
    public boolean isValid(String s) {

        // First split the string 
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();

        for(int i=0;i<chars.length;i++){
            if(chars[i]=='('){
                stack.push(')');
            }else if(chars[i]=='{'){
                stack.push('}');
            }else if(chars[i]=='['){
                stack.push(']');
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                if(stack.peek()==chars[i]){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }

        if(!stack.isEmpty()){
           return false;
        }

       return true;

    }

}

Tags: Algorithm data structure leetcode

Posted by syamswaroop on Sun, 23 Oct 2022 12:33:26 +0530