The 19th day of working hard to become a programmer

Posted by ConcreteDJ on Sun, 09 Jan 2022 15:58:46 +0100

Today, I was oncall again. Although things are simple, dealing with people's emotions really affects their mood. Adjust the negative emotions, calm down to listen to the class and brush the questions. Speaking about double pointers today, the idea and implementation are relatively simple.

Study notes:
1.167. Sum of two II - input ordered array
Given an integer array numbers that has been arranged in non decreasing order, please find out that the sum of the two numbers in the array is equal to the target number target.

The function should return the subscript values of these two numbers as an array of integers of length 2. The subscript of numbers starts counting from 1, so the answer array should meet 1 < = answer [0] < answer [1] < = numbers length .

You can assume that each input only corresponds to a unique answer, and you can't reuse the same elements.

Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: the sum of 2 and 7 is equal to the target number 9. So index1 = 1, index2 = 2.
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Tips:
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers are in non decreasing order
-1000 <= target <= 1000
Only one valid answer exists

Solution idea: for a very classic twosum problem, the violent solution uses double traversal with time complexity of O (N^2), or binary search with time complexity of O (log N). It mainly introduces the use of double pointers, one at the beginning and one at the end. If the sum is greater than target, the right pointer minus 1, less than target, and the left pointer plus 1 Time complexity is O (N)

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] result = new int[2];
        int left = 0;
        int right = numbers.length-1;
        while(left < right){
            if ((numbers[left]+numbers[right]) == target){
                result[0] = left+1;
                result[1] = right+1;
                break;
            }
            else if ((numbers[left]+numbers[right])>target){
                right--;
            }
            else if ((numbers[left]+numbers[right])<target){
                left++;
            }
        }
        return result;
    }
}
  1. leetcode125 validate palindrome string
    Given a string, verify whether it is a palindrome string. Only alphanumeric characters are considered, and the case of letters can be ignored.

Note: in this topic, we define an empty string as a valid palindrome string.

Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Explanation: "Amana planacanalpanama" is a palindrome string
Example 2:
Enter: "race a car"
Output: false
Explanation: "raceacar" is not a palindrome string
Tips:
1 <= s.length <= 2 * 10^5
The string s consists of ASCII characters

Problem solving idea: use the left and right pointers to traverse the string from the beginning and end respectively. If you encounter special characters, skip, convert the case, and compare whether the letters are the same. If the two pointers coincide and the letters are the same, it indicates that it is a palindrome string, otherwise it is not

class Solution {
    public boolean isPalindrome(String s) {
        int left = 0;
        int right = s.length()-1;
        while(left<right){
            
            while((left<right) && (!Character.isLetterOrDigit(s.charAt(left)))){
       
                left++;

            }
            while((left<right) && (!Character.isLetterOrDigit(s.charAt(right)))){
               
                right--;
            }
            if((left<right) && (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right)))){
                return false;
            }
            else{
                left++;
                right--;
            }
        }
        return true;
    }
}

3.344. Reverse string
Write a function that inverts the input string. The input string is given as a character array s.

Do not allocate additional space to another array. You must modify the input array in place and use the additional space of O(1) to solve this problem.

Example 1: input: s = ["h", "e", "l", "l", "o"] output: ["o", "l", "l", "e", "h"]
Example 2: input:
s= ["H","a","n","n","a","h"]
Output: ["H", "a", "n", "n", "a", "H"]
Tip: 1 < = s.length < = 10 ^ 5 s [i] are printable characters in ASCII code table

Solution idea: set two pointers at the beginning and the end, and start traversal respectively. When left < right, exchange the element values pointed to by the two pointers.

class Solution {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length-1;
        while(left < right){
            char tmp;
            tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
            left ++;
            right --;
        }
    }
}
    1. Inverts vowels in a string
      Give you a string s, only invert all vowels in the string, and return the result string.
      Vowel letters include 'a', 'e', 'i', 'o' and 'u', and may appear in both case and upper case.

Example 1: Enter: s = "hello"
Output: "hole"
Example 2: Enter: s = "leetcode"
Output: "leotcede"
Tip: 1 < = S. length < = 3 * 10 ^ 5 S consists of printable ASCII characters

Solution: convert the String into char [] format, set the left and right pointers, and start traversal respectively to determine whether it is a vowel letter. If so, exchange the values of the two positions. After traversal, create a new String and assign the value of char [] to it

class Solution {
    public String reverseVowels(String s) {
        int left = 0;
        int right = s.length()-1;
        char[] result = s.toCharArray();
        while(left < right){
            while((left<right)&&(result[left]!='a' && result[left]!='e' && result[left]!='i' && result[left]!='o' && result[left]!='u' && result[left]!='A'&& result[left]!='E' && result[left]!='I'&& result[left]!='O'&& result[left]!='U')){
                left ++;
            }
            while((left<right)&&(result[right]!='a' && result[right]!='e' && result[right]!='i' && result[right]!='o' && result[right]!='u' && result[right]!='A'&& result[right]!='E' && result[right]!='I'&& result[right]!='O'&& result[right]!='U')){
                right --;
            }
            if( left<right ){
                char tmp;
                tmp = result[left];
                result[left] = result[right];
                result[right] = tmp;
                left ++;
                right --;
            }
        }
        String sr = String.valueOf(result);
        return sr;
    }
}

Today's double pointer is relatively simple. I have brushed one more question and another thinking question 11. I always feel that there is too little spare time. I should keep enough sleep time, exercise and keep healthy. It still consumes a lot to brush questions while working full-time. It will be updated continuously tomorrow.

Topics: Algorithm leetcode