classSolution { public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); for (inti=0; i < chars.length; i += 2*k) { intleft= i; intright= Math.min(i + k - 1, chars.length - 1); while (left < right) { chartemp= chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } } returnnewString(chars); } }
1 2 3 4 5 6 7 8 9 10 11 12 13
funcreverseStr(s string, k int)string { ss := []byte(s) for i := 0; i < len(ss); i += 2 * k { left := i right := min(left+k-1, len(ss)-1) for left < right { ss[left], ss[right] = ss[right], ss[left] left++ right-- } } returnstring(ss) }
Replace numbers
Given a string s that contains lowercase alphabetic and numeric characters, write a function that leaves the alphabetic characters in the string unchanged and replaces each numeric character with a number. For example,
Input:
1
a1b2c3
Output:
1
anumberbnumbercnumber
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package main
import"fmt"
funcmain() { var strByte []byte
fmt.Scanln(&strByte)
for i := 0; i < len(strByte); i++ { if strByte[i] <= '9' && strByte[i] >= '0' { insertElement := []byte{'n', 'u', 'm', 'b', 'e', 'r'} strByte = append(strByte[:i], append(insertElement, strByte[i+1:]...)...) i = i + len(insertElement) - 1 } }