"Leetcode" double pointer

question

Topics (7 in total):
167. Sum of Two Numbers II - Input Sorted Array
633. Sum of Square Numbers
345. Reverse Vowel Characters in a String
680. Verify Palindrome II
88. Merge Two Sorted Arrays
141. Ring Linked List
524. Match to longest word in dictionary by deleting letters

167. Sum of Two Numbers II - Entering an Ordered Array - Ideas

Motif as a double pointer type.

167. Grammar

Java uses the array: int[] numbers, corresponding to the length of the array numbers.length.
It involves the difference between length and length() in java .
C++ uses containers vector<int>& numbers, and the corresponding length is numbers.size().

633. Sum of Square Numbers - Ideas

The key to this question is the initialization of the right pointer. Let the right pointer be x and the left pointer be fixed at 0. In order to make the value of 0^2 + x*x as close as possible to target, we can take x as sqrt(target). You only need to traverse 0~sqrt(target) once at most, so the time complexity is O(sqrt(target)). And because only two extra variables are used, the space complexity is O(1).

633. Grammar

Square root function:
Java: long right = (long) Math.sqrt(c);
c++:long right = (int)sqrt(c);

345. Reverse Vowel Characters in a String - Syntax

Java version:

 char[] arr = s.toCharArray();
 //String字符串变成char数组
public boolean isVowel(char ch) {
    
    
        return "aeiouAEIOU".indexOf(ch) >= 0;
    }

Usage of indexOf() function in java

680. Verify Palindrome II-Thoughts

The greedy algorithm is used, the code is written, and it can be AC, but I have no concept of the greedy algorithm. When I get greedy, I need to review this question.

88. Merge two sorted array ideas

My idea: use double pointers, in order to make the space complexity O(1), num1 and num2 are directly merged into num1, but there is a problem: some elements of num1 are covered.
Improvement: It can be seen from observation that the second half of nums1 is empty and can be directly overwritten without affecting the result. Therefore, the pointer can be set to traverse from the back to the front , and each time the larger of the two is taken and put into the end of nums1.

The difference between JAVA HashMap and HashSet

141. Circular Linked List

Method 1-hash table: use the property that the hash table cannot have repeated elements.
Method 2 - Fast and Slow Pointers: This topic is the classic motif of fast and slow pointers.

524. Match Longest Word in Dictionary by Removing Letters

Idea: Decompose the topic into two steps: 1. Find the longest string; 2. Determine whether t is a substring of s.
code (java):

class Solution {
    
    
    public String findLongestWord(String s, List<String> dictionary) {
    
    
        //循环遍历目标串数组,查找符合条件的最大字串元素
        String longestWord="";

        
        for(String target:dictionary){
    
    

            //1. 找最长串
            int l1=longestWord.length(),l2=target.length();
            if(l1>l2 || (l1==l2 && longestWord.compareTo(target)<0)){
    
    
                continue;//如果当前最长串长度l1比较大or长度相同但字母列比较小,则仍然为当前字串元素为结果
            }

            //2. 判断是否为s子串
            if(vaildString(s,target)){
    
    
                longestWord=target;
            }
        }
        return longestWord;
    }

    public boolean vaildString(String s,String t){
    
    
        //判断字符串t是否为字符串s的子串
        int i=0,j=0;
        while(i<s.length() && j<t.length()){
    
    
            if(s.charAt(i)==t.charAt(j)){
    
    
                j++;
            }
            i++;
            //若果不相等,则目标串j不动,固定串s的下标i右移一位
        }
        return j==t.length();//判断目标串是否遍历完
    }
}

java compareTo() method

Guess you like

Origin blog.csdn.net/JingYan_Chan/article/details/127568039
Recommended