slow := 0 for i := 0; i < len(b); i++ { if b[i] != ' ' { if slow != 0 { b[slow] = ' ' slow++ } for i < len(b) && b[i] != ' ' { b[slow] = b[i] slow++ i++ } } } b = b[0:slow]
reverse(b)
last := 0 for i := 0; i <= len(b); i++ { if i == len(b) || b[i] == ' ' { reverse(b[last:i]) last = i + 1 } } returnstring(b) }
funcreverse(b []byte) { left := 0 right := len(b) - 1 for left < right { b[left], b[right] = b[right], b[left] left++ right-- } }
Right-rotation operation of a string
The right-rotation operation of a string involves shifting a number of characters from the end of the string to the front of the string. Given a string s and a positive integer k, write a function that implements the right-rotation operation on a string by shifting the trailing k characters in the string to the front of the string. For example,