当前位置:网站首页>Sum of digits under k-ary representation of leetcode simple problem
Sum of digits under k-ary representation of leetcode simple problem
2022-06-29 05:53:00 【·Starry Sea】
subject
Give you an integer n(10 Base number ) And a cardinality k , Would you please n from 10 The hexadecimal representation is converted to k Hexadecimal said , Calculate and return the... Of each number after conversion The sum of the .
After the transformation , Your numbers should be taken as 10 Hexadecimal Numbers , And the sum of them should be 10 Hexadecimal means to return .
Example 1:
Input :n = 34, k = 6
Output :9
explain :34 (10 Base number ) stay 6 It is expressed as 54 .5 + 4 = 9 .
Example 2:
Input :n = 10, k = 10
Output :1
explain :n Itself is 10 Base number . 1 + 0 = 1 .
Tips :
1 <= n <= 100
2 <= k <= 10
source : Power button (LeetCode)
Their thinking
Analog decimal to other decimal conversion process , And add the numbers of each bit .
class Solution:
def sumBase(self, n: int, k: int) -> int:
s=0 # Count the result of adding each bit
while n>0:
s+=n%k
n//=k
return s

Of course, it can also be realized by recursion , But the efficiency will decrease .
class Solution:
def sumBase(self, n: int, k: int) -> int:
if n<k:
return n
else:
return n%k+self.sumBase(n//k,k)

边栏推荐
- The translation of those exquisite lines in the eighth season of the big bang
- CCTV revealed that xumengtao won the black Technology: there was a virtual coach???
- Openfpga wishes you a happy Lantern Festival!
- data management plan
- Establishing the development environment of esp8266
- Research on heuristic intelligent task scheduling
- 想问问,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
- [chromium] win10 vs2019 environment chromium configuration and compilation.
- Why can't the article be posted?
- Love that can't be met -- what is the intimate relationship maintained by video chat
猜你喜欢

Devops development, operation and maintenance Basics: using Jenkins to automatically build projects and notify by email

HTTP Caching Protocol practice

2022 community group buying industry research industry development planning prospect investment market analysis report (the attachment is the online disk link, and the report is continuously updated)

Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)

2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up

VLAN experiment

Analysis report on the investment market of the development planning prospect of the recommended wind power industry research industry in 2022 (the attachment is a link to the network disk, and the re

There are two ways for golang to develop mobile applications

2022 recommended trend toy blind box industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuously up

PCI Verilog IP
随机推荐
C語言用 printf 打印 《愛心》《火星撞地球》等,不斷更新
Modularization and modular specification commonjs
Slot
嵌入式RTOS
Why can't the article be posted?
Top ten Devops best practices worthy of attention in 2022
HTTP Caching Protocol practice
The fresh student who was born in Ali after 2000: it's really fragrant to mend this
Alphacode made its debut! The programming version of "Alpha dog" competed quietly and defeated half of the programmers
Leetcode theme [array] -219- duplicate Element II exists
Can use the mouse, will reinstall the computer system tutorial sharing
The easiest GUI to compile: dgui
Spark saving to external data source
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
Conditional test, if and case conditional test statements of shell script
Use of sed in shell script
5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin ((tdmpp) H2) /2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin copper (no2tdmpp) Cu) supplied by Qiyue
[high concurrency] deeply analyze the callable interface
Summary of redis basic knowledge points
Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)