Question 1 rotate array
Topic information
Give you an array, rotate the elements in the array to the right K , positions, where k , is a non negative number.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
Turn right for one step: [7,1,2,3,4,5,6]
Turn right for 2 steps: [6,7,1,2,3,4,5]
Rotate right for 3 steps: [5,6,7,1,2,3,4]
Example 2:
Input: num = [- 1, - 100,3,99], k = 2
Output: [3,99, - 1, - 100]
Explanation:
Turn right for 1 step: [99, - 1, - 100,3]
Rotate right for 2 steps: [3,99, - 1, - 100]
Tips:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
Advanced:
Think of as many solutions as possible. There are at least three different ways to solve this problem.
Can you solve this problem by using the ^ algorithm with spatial complexity ^ O(1)?
Problem solving ideas
This example is introduced in slice of go language Bible
The reverse function below reverses the slice of type [] int in the original memory space, and it can be used for slices of any length.
// reverse reverses a slice of ints in place.
func reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] }
}
Here we apply the inversion array:
a := [...]int{0, 1, 2, 3, 4, 5}
reverse(a[:])
fmt.Println(a) // "[5 4 3 2 1 0]"
One way to rotate slice elements to the left is to call the reverse inversion function three times. The first time is to invert the first n elements, then the remaining elements, and finally the elements of the whole slice. (if the rotation is to the right, move the third function call to the first call position.)
s := []int{0, 1, 2, 3, 4, 5}
// Rotate s left by two positions.
reverse(s[:2])
reverse(s[2:])
reverse(s)
fmt.Println(s) // "[2 3 4 5 0 1]"
Therefore, using the reverse function, we can easily complete any right rotation
Detailed code
func rotate(nums []int, k int) { n:=len(nums) if k>=n { k=k%n } if n==1{ fmt.Println(nums) }else{ reverse(nums[n-k:n]) reverse(nums[:n-k]) reverse(nums) fmt.Println(nums) } } func reverse(s []int) { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] } }
Code optimization
Quoted from official analysis
func reverse(a []int) { for i, n := 0, len(a); i < n/2; i++ { a[i], a[n-1-i] = a[n-1-i], a[i] } } func rotate(nums []int, k int) { k %= len(nums) reverse(nums) reverse(nums[:k]) reverse(nums[k:]) } Author: LeetCode-Solution Link: https://leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.
Complexity analysis
Time complexity: O(n), where n is the length of the array. Each element is flipped twice, a total of n elements, so the total time complexity is O(2n)=O(n).
Space complexity: O(1).
Another solution
See official analytical method 2: Ring replacement
Note: for nk=lcm(n,k)gcd(n,k)
have AB = xaxb,because x Is the greatest common factor, so a,b Coprime. Using short division, it is easy to get xab by AB The least common multiple of is proved.*
There are duplicate elements in question 2
Topic information
Given an integer array, determine whether there are duplicate elements.
If a value exists and appears in the array at least twice, the function returns true. Returns false if each element in the array is different.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
Problem solving ideas
Traverse the array and store the array element information in the hash table. Get the required information by looking up the table
code
func containsDuplicate(nums []int) bool { m := map[int] int{} for _,num:= range nums{ m[num]++ } for _, frequency := range m { if frequency > 1{ return true } } return false }
Complexity analysis
Time complexity: O(N), where N is the length of the array.
Space complexity: O(N), where N is the length of the array.