Basic learning of algorithm base conversion (Introduction)

Coca Cola 2021-11-25 14:44:16

Catalog

Algorithm learning

《 Algorithm zero basis 100 speak 》( The first 19 speak ) Hexadecimal conversion ( One ) - introduction _ Where do heroes come from -CSDN Blog https://blog.csdn.net/WhereIsHeroFrom/article/details/120875977

Problem solving report

Example 1

Example 2

Example 3

Example 4

Example 5

Example 6

Example 7


Algorithm learning


《 Algorithm zero basis 100 speak 》( The first 19 speak ) Hexadecimal conversion ( One ) - introduction _ Where do heroes come from -CSDN Blog icon-default.png?t=LA46https://blog.csdn.net/WhereIsHeroFrom/article/details/120875977

Problem solving report


Example 1

Binary 1 The number of icon-default.png?t=LA46https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/

  • Title Description :

  • Their thinking :

More basic , The use of remainder and division

  • Reference code :
nt hammingWeight(uint32_t n) {
int i,cnt=0;
while(n)
{
if(n%2==1)
cnt++;
n/=2;
}
return cnt;
}

Example 2

Members add icon-default.png?t=LA46https://leetcode-cn.com/problems/add-digits/

  • Title Description :

  • Their thinking :

In general, using loops can solve

If it is advanced , You need a little mathematical thought :

Here, let's express a three digit number as xyz, Then there is x*100+y*10+z=x*99+y*9+x+y+z

In order to get x+y+z Then the whole %9

The result of this question must be 0-9, If %9 The result is 0-8, Then take out one first 1 Plus 1:

(x*99+y*9+x+y+z+1-1)%9=(x+y+z-1)%9+1%9=(x+y+z-1)%9+1

  • Reference code :
int addDigits(int num){
return (num-1)%9+1;
}

Example 3

1290. Binary linked list to integer - Power button (LeetCode) (leetcode-cn.com)icon-default.png?t=LA46https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer/

  • Title Description :

  • Their thinking :

Traversal of the list + The idea of base system

  • Reference code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getDecimalValue(struct ListNode* head){
int ret=0;
struct ListNode*cur=head;
while(cur)
{
ret=ret*2+cur->val;
cur=cur->next;
}
return ret;
}

Example 4

1837. K The sum of each digit in hexadecimal representation - Power button (LeetCode) (leetcode-cn.com)icon-default.png?t=LA46https://leetcode-cn.com/problems/sum-of-digits-in-base-k/

  • Title Description :
  • Their thinking :

Hexadecimal conversion + Sum the numbers ( Remainder and division )

  • Reference code :
int sumBase(int n, int k){
int ret=0;
while(n)
{
ret=ret*10+n%k;
n/=k;
}// The number obtained is the reverse , But it's not in the way
int ans=0;
while(ret)// Simply sum the digits
{
ans+=ret%10;
ret/=10;
}
return ans;
}

Example 5

1399. Count the number of the largest groups - Power button (LeetCode) (leetcode-cn.com)icon-default.png?t=LA46https://leetcode-cn.com/problems/count-largest-group/

  • Title Description :

  • Their thinking :

Record the number of each sum with a tag array , Record the maximum number at the same time , Then traverse the count

  • Reference code :
int countLargestGroup(int n){
int book[50]={0};// Mark each number and array ( Each number and has a range )
int max=0;// Record the maximum number of pieces
int i;
for(i=1;i<=n;i++)
{
int sum=0;
int tmp=i;
while(tmp)// Sum of numbers
{
sum+=tmp%10;
tmp/=10;
}
book[sum]++;// Number of tags and
if(book[sum]>max)// Mark max
max=book[sum];
}
int cnt=0;// Traversal count
for(i=1;i<50;i++)
{
if(book[i]==max)
cnt++;
}
return cnt;
}

Example 6

504. Octal number - Power button (LeetCode) (leetcode-cn.com)icon-default.png?t=LA46https://leetcode-cn.com/problems/base-7/

  • Title Description :

  • Their thinking :

Algorithm learning has been explained

  • Reference code :
char * convertToBase7(int num){
char*ret=(char*)malloc(sizeof(char)*20);
int stack[20]={0},top=0;// Array stack
int retsize=0;
if(num<0)// The sign
{
ret[retsize++]='-';
num=-num;
}
if(num==0)// A special case
{
return "0";
}
else
{
while(num)// Reverse storage of hexadecimal numbers
{
stack[top++]=num%7;
num/=7;
}
}
while(top--)
{
ret[retsize++]=stack[top]+'0';// Reverse conversion
}
ret[retsize]='\0';// Terminator
return ret;
}

Example 7

405. Convert numbers to hexadecimal numbers - Power button (LeetCode) (leetcode-cn.com)icon-default.png?t=LA46https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/

  • Title Description :

  • Their thinking :

Just change it on the basis of the above question

  • Reference code :
char * toHex(int num){
char*ret=(char*)malloc(sizeof(char)*20);
int stack[20]={0},top=0;// Array stack
int retsize=0;
unsigned int n=num;// Complement operation
if(n==0)// A special case
{
return "0";
}
else
{
while(n)// Reverse storage of hexadecimal numbers
{
stack[top++]=n%16;
n/=16;
}
}
while(top--)
{
if(stack[top]>=10)
{
ret[retsize++]=stack[top]-10+'a';
}
else
{
ret[retsize++]=stack[top]+'0';// Reverse conversion
}
}
ret[retsize]='\0';// Terminator
return ret;
}

Please bring the original link to reprint ,thank
Similar articles

2021-11-25