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: nums = [1,2,3,4] Output:[1,3,2,4] Note:[3,1,2,4] It is also one of the correct answers.
Tips:
- 0 <= nums.length <= 50000
- 1 <= nums[i] <= 10000
class Solution { public: vector<int> exchange(vector<int>& nums) { int l = 0, r = nums.size() - 1; while(l < r){ while(nums[l] % 2 != 0 && l < r)l ++; while(nums[r] % 2 == 0 && r > l)r --; if(l < r)swap(nums[l], nums[r]); l ++; r --; } return nums; } };
Algorithm idea
This problem is relatively simple. Before odd numbers are placed and after even numbers are placed, you can use two pointers to point to the head and tail of the array. The head pointer moves backward to find even numbers, and then the back pointer moves forward to find odd numbers, and then exchange them.
Sword finger Offer 57. And are 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: nums = [2,7,11,15], target = 9 Output:[2,7] perhaps [7,2]
Example 2:
Input: nums = [10,26,30,31,47,60], target = 40 Output:[10,30] perhaps [30,10]
Limitations:
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^6
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { int l = 0, r = nums.size() - 1; while(l < r && nums[l] + nums[r] != target){ if(nums[l] + nums[r] < target){ l ++; }else r --; } if(l >= r){ return {}; } return {nums[l], nums[r]}; } };
Algorithm idea
The array of this problem is in good order, so you can also use double pointers. The two pointers point to the head and tail, compare the sum of the indexes of the two pointers with the size of the target, and then move the head pointer backward (and larger) or the tail pointer forward (and smaller).
Sword finger Offer 58 - I. flip 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:
input: " hello world! " output: "world! hello" explain: The input string can contain extra spaces before or after, but the inverted characters cannot be included.
Example 3:
input: "a good example" output: "example good a" explain: 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.
class Solution { public: string reverseWords(string s) { int n = s.size(), l = 0, r = n - 1; while(l < n && s[l] == ' ')l ++; while(r >= 0 && s[r] == ' ')r --; string ans = ""; while(l <= r){ int i = r; while(i >= l && s[i] != ' ')i --; i ++; for(int j = i; j <= r; ++ j)ans += s[j]; if(l < i)ans += ' '; i --; while(i >= l && s[i] == ' ' )i --; r = i; } return ans; } };
Algorithm idea
First remove the excess spaces at the beginning and end of the string, and then traverse from the end of the string. Find each word one by one and fill in ans.
[sword finger Offer] series:
[sword finger Offer] stack
[sword finger Offer] linked list
[sword finger Offer] string
[sword finger Offer] search algorithm
[sword finger Offer] search algorithm (1)
[sword finger Offer] search and backtracking algorithm
[sword finger Offer] search and backtracking algorithm (1)
[sword finger Offer] dynamic programming
[sword finger Offer] dynamic programming (1)
[sword finger Offer] dynamic programming (2)
[sword finger Offer] double pointer
[sword finger Offer] double pointer (1)