【每日一题20220323】统计数字

:mage:‍给定一个正整数n和一个目标数字target,请编写一个函数,将0到n之间(包含首尾)的所有数字进行平方,统计并返回这些平方数中包含target目标数的总个数。如果平方数中含出现多次目标数字,应当统计多次。

示例:
输入:n=10, target = 1
输出:4
解释:平方后得到 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100,含有1的数字有1,16,81和100,所以是返回4。

题目难度:简单
题目来源:CodeWars:Count the Digit

def solution(n: int, target: int) -> int:
    # your code here

assert solution(10, 1) == 4
assert solution(25, 1) == 11
assert solution(5750, 0) == 4700
assert solution(12224, 8) == 7733
def solution(n: int, target: int) -> int:
    return ''.join([str(i**2) for i in range(n + 1)]).count(str(target))
def solution(n: int, target: int) -> int:
    return ''.join([str(i ** 2) for i in range(n + 1)]).count(str(target))


assert solution(10, 1) == 4
assert solution(25, 1) == 11
assert solution(5750, 0) == 4700
assert solution(12224, 8) == 7733

有点不理解,按照题目的意思,是平方后所得的数字的中含有1的,solution(25, 1)是不是应该只有10个呢
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625]
10个有1,16,81,100,121,144,169,196,361,441


def count_num(n:int,target:int)->int:
    list_1=[i**2 for i in range(0,n+1)]
    list_2=[i for i in list_1 if str(target) in str(i)]
    return len(list_2)

实际统计的是所有数平方出现1的次数,121出现两次1,算两次

是的 对对,谢谢哈

def solution(n: int, target: int) -> int:
    return ''.join([str(i ** 2) for i in range(n + 1)]).count(str(target))


assert solution(10, 1) == 4
assert solution(25, 1) == 11
assert solution(5750, 0) == 4700
assert solution(12224, 8) == 7733

:grinning: :grinning: :grinning:

def solution(n: int, target: int) -> int:
   return ''.join([str(i*i) for i in range(n+1)]).count(str(target))
    def solution(n: int, target: int) -> int:
        return len(''.join([str(i**2) for i in range(n+1)]).split(str(target))) - 1
def solution(n: int, target: int) -> int:
    # your code here
    ls = [ str(i**2).count(str(target)) for i in  range(n+1) if str(target) in str(i**2)]
    return sum(ls)

if __name__ == '__main__':

    assert solution(10, 1) == 4
    assert solution(25, 1) == 11
    assert solution(5750, 0) == 4700
    assert solution(12224, 8) == 7733
def solution(n: int, target: int) -> int:
    pf_list = [i*i for i in range(n + 1)]

    count = 0
    for num in pf_list:
        c = str(num).count(str(target))
        count += c
    return count
 @Test
    void test02(){
       assertEquals(testCase_06(10,1),4);
       assertEquals(testCase_06(25,1),11);
       assertEquals(testCase_06(5750,0),4700);
       assertEquals(testCase_06(12224,8),7733);
    }
  public  int testCase_06(int n,int target) {
        //把将0到n之间(包含首尾)的所有数字进行平方放在集合中
      List<Integer> list = new ArrayList<>();
      for (int i = 0; i <= n; i++) {
          list.add(i * i);
      }
      //统计目标数组出现的次数
         int res = 0;
         for (int i =0;i<list.size();i++){
          for (int j = list.get(i); j > 0; j = j / 10) {//j>0于一个数等于0.
              if (j % 10 == target) {
                  res++;
              }
          }
         }
         if(target==0){
             res = res+1;
         }
      return res;
  }
def solution(n, target):
    list_n = []
    list_target = []
    target_num = 0
    target_str = str(target)
    for i in range(n+1):
        num_str = str(i**2)
        list_n.append(num_str)
        list_target.extend(num_str)
        if target_str in list_n[i]:
            for j in range(len(list_target)):
                if target_str == list_target[j]:
                    target_num += 1
        list_target.clear()

    return target_num
def solution(n: int, target: int) -> int:
	count=0
	for i in range(n+1):
		count=count+str(i**2).count(str(target))
	return count

assert solution(10, 1) == 4
assert solution(25, 1) == 11
assert solution(5750, 0) == 4700
assert solution(12224, 8) == 7733
def solution(n: int, target: int) -> int:
    return ''.join([i for i in [str(i**2) for i in range(0,n+1)] if str(target) in i]).count(str(target))

assert solution(10, 1) == 4
assert solution(25, 1) == 11
assert solution(5750, 0) == 4700
assert solution(12224, 8) == 7733