func reverseLeftWords(s string, n int) string {
if len(s) <= n {
return s
}
return s[n:] + s[:n]
}
简单题,string不就是切片吗
func relativeSortArray(arr1 []int, arr2 []int) []int {
res := []int{}
tem := []int{}
for idx, item := range arr2 {
for _, num := range arr1 {
if num == item {
res = append(res, num)
} else {
tem = append(tem, num)
}
}
if idx == len(arr2)-1 {
sort.Ints(tem)
res = append(res, tem...)
break
}
arr1 = tem
tem = []int{}
}
return res
}
也不错,这道题目还是有点意思的
3.剑指 Offer 53 - I. 在排序数组中查找数字 I
func search(nums []int, target int) int {
res := 0
if len(nums) == 0 {
return 0
}
for _, num := range nums {
if num == target {
res += 1
}
}
return res
}
没问题