How to overcome the dilemma of doing Leetcode questions

How to overcome the dilemma of doing Leetcode questions

problem background

Obviously I learned a lot of knowledge consciously, but when I actually started to do Leetcode problems, I still had the dilemma of "one pen, one hand, one Leetcode for one night". The question types are not too difficult, and you can figure out the solution by reading the questions, but when you do it yourself, you just look at the questions and have no way to start. This dilemma is troubling you now. Have you ever had such an experience?

Advice for Overcoming Difficulties

insert image description here

Combination of practice and theory

Theoretical knowledge is only part of it, and applying it to practical problems is the key to learning. Doing questions is the best way to consolidate knowledge, so practice more and do more.

Avoid rote memorization

During the learning process, make sure to understand the concepts and problem-solving ideas instead of memorizing them by rote. Comprehension is the foundation of long-term memory.

Analyzing problem-solving ideas

Before solving a problem, read the problem carefully to understand the requirements and limitations of the problem. Try to build a problem-solving idea in your mind and break down the problem into smaller sub-problems.

import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    
    

    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);
        }

        return new int[0]; // 如果没有找到符合条件的两个数,返回空数组
    }

    public static void main(String[] args) {
    
    
        int[] nums = {
    
    2, 7, 11, 15};
        int target = 9;
        TwoSum solution = new TwoSum();
        int[] result = solution.twoSum(nums, target);
        if (result.length == 2) {
    
    
            System.out.println("找到符合条件的两个数的索引为:" + result[0] + ", " + result[1]);
        } else {
    
    
            System.out.println("未找到符合条件的两个数!");
        }
    }
}

This Java code example solves the sum of two numbers problem on Leetcode by using HashMap.

First, traverse the given integer array nums, and for each element, calculate the difference between it and the target targetvalue, and use the difference as the key and the index of the current element as the value, and store it in the HashMap. During the traversal process, check whether the complement of the current element exists in the HashMap, if it exists, it means that two numbers that meet the conditions have been found, and return their indexes; if not, continue traversing and add the current element to the HashMap middle. If the two numbers that meet the conditions are not found after the traversal is complete, an empty array is returned.

In mainthe method, we create an example array numsand the target value target, then call twoSumthe method to get the result, and output the result.

Note : This is just a simple sample code. When actually solving Leetcode problems, more boundary cases and optimization methods need to be considered.

don't read the answer prematurely

Don't prematurely look at the answers or solutions before trying to solve the problem yourself. Think more, try more, and constantly adjust your thinking.

iterative learning

If a question cannot be solved for a while, you can skip it first and continue to do the following questions. Subsequent questions may give you new ideas or revelations before you go back and try previous puzzles.

ask for help

If you get stuck on a problem, you can

Seek out a teacher or seek answers on a learning platform. Communicating with others and sharing ideas often leads to new ideas.

persistence and patience

Doing questions is a process of gradual improvement. Don't be discouraged because you can't solve the problem for a while. Be patient and keep practicing, and you will gradually break through and make progress.

Filling in the gaps

In doing the questions, you may find that you do not have a solid grasp of certain knowledge points. Timely record and check for omissions and fill vacancies to fill in knowledge gaps.

in conclusion

If you keep practicing, you will gradually break through the difficulties and make progress.

Guess you like

Origin blog.csdn.net/apple_67445472/article/details/131794594