0001~0010
/**
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
func Aplusb(a int, b int) int {
// write your code here
return a + b
}
/**
* @param k: An integer
* @param n: An integer
* @return: An integer denote the count of digit k in 1..n
*/
import (
"strconv"
"strings"
)
func DigitCounts(k int, n int) int {
// write your code here
//思路:0-n转换为字符串切片
src := ""
target := strconv.Itoa(k)
//把0-n的数字转化为字符串添加到切片当中
for i := 0; i <= n; i++ {
src += strconv.Itoa(i)
}
//查找对应出现的次数
return strings.Count(src, target)
}
import (
"bytes"
"strconv"
"strings"
)
func DigitCounts(k int, n int) int {
// write your code here
var strs string
var buffer bytes.Buffer
for i := 0; i <= n; i++ {
buffer.WriteString(strconv.Itoa(i))
}
strs = buffer.String()
return strings.Count(strs, strconv.Itoa(k))
}
0011~0020
/**
* @param source:
* @param target:
* @return: return the index
*/
import "strings"
func StrStr(source string, target string) int {
// Write your code here
return strings.Index(source,target)
}
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
func BinarySearch(nums []int, target int) int {
// write your code here
l,r := 0,len(nums)-1
m := l + (r - l) >> 1
for l + 1 < r{
//这边必须是target > nums[m]
if target > nums[m]{
l = m
}else {
r = m
}
m = l + (r - l) >> 1
}
if nums[l] == target {
return l
}
if nums[r] == target {
return r
}
return -1
}
0021~0030
/**
* @param c: A character.
* @return: The character is alphanumeric or not.
*/
func IsAlphanumeric(c byte) bool {
// write your code here
if c <= 'z' && c >= 'a' || c <= 'Z' && c >= 'A' || c <= '9' && c >= '0'{
return true
}
return false
}
0031~0040
/**
* @param number: A 3-digit number.
* @return: Reversed number.
*/
func ReverseInteger(number int) int {
// write your code here
i := number % 10
j := number / 10 % 10
k := number / 100
return i * 100 + j * 10 + k
}
0041~0050