0001~0010

  1. /**
  2. * @param a: An integer
  3. * @param b: An integer
  4. * @return: The sum of a and b
  5. */
  6. func Aplusb(a int, b int) int {
  7. // write your code here
  8. return a + b
  9. }
  1. /**
  2. * @param k: An integer
  3. * @param n: An integer
  4. * @return: An integer denote the count of digit k in 1..n
  5. */
  6. import (
  7. "strconv"
  8. "strings"
  9. )
  10. func DigitCounts(k int, n int) int {
  11. // write your code here
  12. //思路:0-n转换为字符串切片
  13. src := ""
  14. target := strconv.Itoa(k)
  15. //把0-n的数字转化为字符串添加到切片当中
  16. for i := 0; i <= n; i++ {
  17. src += strconv.Itoa(i)
  18. }
  19. //查找对应出现的次数
  20. return strings.Count(src, target)
  21. }
  1. import (
  2. "bytes"
  3. "strconv"
  4. "strings"
  5. )
  6. func DigitCounts(k int, n int) int {
  7. // write your code here
  8. var strs string
  9. var buffer bytes.Buffer
  10. for i := 0; i <= n; i++ {
  11. buffer.WriteString(strconv.Itoa(i))
  12. }
  13. strs = buffer.String()
  14. return strings.Count(strs, strconv.Itoa(k))
  15. }

0011~0020

  1. /**
  2. * @param source:
  3. * @param target:
  4. * @return: return the index
  5. */
  6. import "strings"
  7. func StrStr(source string, target string) int {
  8. // Write your code here
  9. return strings.Index(source,target)
  10. }
  1. /**
  2. * @param nums: The integer array.
  3. * @param target: Target to find.
  4. * @return: The first position of target. Position starts from 0.
  5. */
  6. func BinarySearch(nums []int, target int) int {
  7. // write your code here
  8. l,r := 0,len(nums)-1
  9. m := l + (r - l) >> 1
  10. for l + 1 < r{
  11. //这边必须是target > nums[m]
  12. if target > nums[m]{
  13. l = m
  14. }else {
  15. r = m
  16. }
  17. m = l + (r - l) >> 1
  18. }
  19. if nums[l] == target {
  20. return l
  21. }
  22. if nums[r] == target {
  23. return r
  24. }
  25. return -1
  26. }

0021~0030

  1. /**
  2. * @param c: A character.
  3. * @return: The character is alphanumeric or not.
  4. */
  5. func IsAlphanumeric(c byte) bool {
  6. // write your code here
  7. if c <= 'z' && c >= 'a' || c <= 'Z' && c >= 'A' || c <= '9' && c >= '0'{
  8. return true
  9. }
  10. return false
  11. }

0031~0040

  1. /**
  2. * @param number: A 3-digit number.
  3. * @return: Reversed number.
  4. */
  5. func ReverseInteger(number int) int {
  6. // write your code here
  7. i := number % 10
  8. j := number / 10 % 10
  9. k := number / 100
  10. return i * 100 + j * 10 + k
  11. }

0041~0050