[out of order version ● sword finger offer] daily algorithm problem punch in problem solution - double pointer (item No. 21,57,58)

Punch in day9

Question 1: Sword finger Offer 21. Adjust the array order so that odd numbers precede even numbers

Enter an integer array and implement a function to adjust the order of numbers in the array so that all odd numbers are in the first half of the array and all even numbers are in the second half of the array.

Example:
Input: num = [1,2,3,4]
Output: [1,3,2,4]
Note: [3,1,2,4] is also one of the correct answers.

Tips:
0 <= nums.length <= 50000
1 <= nums[i] <= 10000

Source: LeetCode
Link: https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof

Problem solving ideas:
Two pointers are defined. One is left to find even numbers in positive order and the other is right to find odd numbers in reverse order. The even number found by left and the odd number found by right are exchanged.

java code:

class Solution {
    public int[] exchange(int[] nums) {
    	//Define two pointers
        int left = 0, right = nums.length - 1, tmp;
        while(left < right) {//Jump out of loop when two pointers coincide
            while(left < right && (nums[left] % 2) != 0) {
            	left++;//Find even number and jump out
            }
            while(left < right && (nums[right] % 2 ) == 0) {
            	right--;//Find an odd number and jump out
            }
            //exchange
            tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
        }
        return nums;

    }
}

Question 2: Sword finger Offer 57. And two numbers of s

Enter an incrementally sorted array and a number s, and find two numbers in the array so that their sum is exactly s. If the sum of multiple pairs of numbers is equal to s, any pair can be output.

Example 1:
Input: num = [2,7,11,15], target = 9
Output: [2,7] or [7,2]

Example 2:
Input: num = [10,26,30,31,47,60], target = 40
Output: [10,30] or [30,10]

Limitations:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6

Source: LeetCode
Link: https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof

Problem solving ideas:
Note that the array is sorted incrementally, and the left and right pointers are still defined.

java code:

class Solution {
    public int[] twoSum(int[] nums, int target) {
    	//Define double pointers, one left and one right, and go to the middle
        int left = 0, right = nums.length - 1;
        while(left < right) {
            int sum = nums[left] + nums[right];//Calculates the sum of the values of two pointers
            if(sum < target) {//If this sum is less than the target value, the left pointer moves to the right
            	left++;
            }
            else if(sum > target) {//If this sum is greater than the target value, the right pointer moves to the left
            	right--;
            }
            else {//If it is equal to the target value, the left and right pointers are returned
            	return new int[] { nums[left], nums[right] };
            }
        }
        return new int[0];//Returns an empty array
    }
}

Question 3: Sword finger Offer 58 - I. flip the word order

Input an English sentence and flip the order of words in the sentence, but the order of characters in the word remains the same. For simplicity, punctuation is treated like ordinary letters. For example, if you enter the string "I am a student.", you will output "student. a am I".

Example 1:
Input: "the sky is blue"
Output: "blue is sky the"

Example 2:
Enter: "hello world!"
Output: "world! hello"
Explanation: the input string can contain extra spaces before or after, but the inverted characters cannot be included.

Example 3:
Enter: "a good example"
Output: "example good a"
Explanation: if there is extra space between two words, reduce the space between words after inversion to only one.

explain:
Characters without spaces form a word.
The input string can contain extra spaces before or after, but the inverted characters cannot be included.
If there is extra space between two words, reduce the space between words after inversion to only one.

Source: LeetCode
Link: https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof

Problem solving ideas:
Traverse the string s in reverse order, record the left and right index boundaries of the words, and put the obtained words in the list
Finally, the list is spliced back into strings.

java code:

class Solution {
    public String reverseWords(String s) {
        s = s.trim(); // Delete spaces at both ends
        int right = s.length() - 1, left = right;
        StringBuilder res = new StringBuilder();//Define variable string
        while(left >= 0) {
            while(left >= 0 && s.charAt(left) != ' ') {// Search word boundaries
            	left--; 
            }
            res.append(s.substring(left + 1, right + 1) + " "); // Add word
            while(left >= 0 && s.charAt(left) == ' ') {
                 left--; // Skip spaces between words
             }
            right = left; // Character pointing to the end of the next word
        }
        return res.toString().trim(); // Convert to string and return, and remove redundant spaces at both ends
    }
}

Tags: Java Algorithm leetcode Double Pointer

Posted on Mon, 20 Sep 2021 05:46:23 -0400 by venradio