当前位置:网站首页>[leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
[leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
2022-07-27 09:15:00 【Super Daxiong】
subject : position 1 The number of
Catalog
subject : position 1 The number of
subject : The difference between the sum of the products of integers
Write a function , Input is an unsigned integer ( In the form of a binary string ), Returns the number of digits in its binary expression as '1' The number of ( Also known as Han Ming weight ).
Tips :
Please note that , In some languages ( Such as Java) in , There is no unsigned integer type . under these circumstances , Both input and output will be specified as signed integer types , And should not affect your implementation , Because whether integers are signed or unsigned , Its internal binary representation is the same .
stay Java in , The compiler uses binary complement notation to represent signed integers . therefore , Above Example 3 in , The input represents a signed integer -3.
Example 1:
Input :00000000000000000000000000001011
Output :3
explain : Binary string of input 00000000000000000000000000001011 in , There are three of them '1'.
Example 2:Input :00000000000000000000000010000000
Output :1
explain : Binary string of input 00000000000000000000000010000000 in , There is one in all for '1'.
Example 3:Input :11111111111111111111111111111101
Output :31
explain : Binary string of input 11111111111111111111111111111101 in , share 31 Position as '1'.
Tips :
The input must be of length 32 Of Binary string .
Advanced :
If you call this function more than once , How will you optimize your algorithm ?
analysis :
I use it. Integer.BinaryString Convert the decimal system into binary system, and then convert it into a string. Loop through and intercept each character to determine whether it is 1 The way of accumulation , I think this method is cumbersome . So I saw the solution of other big guys and used Integer.bitCount();bigCount What is it? ? Let's take a look at the original code
public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}bigCount(): Return to binary complement 1 Number of bits . Using this function, you can directly find the number of one in binary ! At present, I don't understand the bit operation , Welcome the boss to provide valuable guidance .
Solution 1 Code :
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}Solution 2 Code :
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
String s=Integer.toBinaryString(n);
int sum=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='1')
sum++;
}
return sum;
}
}subject : The difference between the sum of the products of integers
Give you an integer n, Please help calculate and return the integer 「 The product of your numbers 」 And 「 The sum of your numbers 」 Difference .
Example 1:
Input :n = 234
Output :15
explain :
Product of numbers = 2 * 3 * 4 = 24
The sum of your numbers = 2 + 3 + 4 = 9
result = 24 - 9 = 15
Example 2:Input :n = 4421
Output :21
explain :
Product of numbers = 4 * 4 * 2 * 1 = 32
The sum of your numbers = 4 + 4 + 2 + 1 = 11
result = 32 - 11 = 21Tips :
1 <= n <= 10^5
analysis :
We can use %10 Take the end ,/10 Cut the end of the method to get the number of each position and then multiply 、 Add it up .
Code :
class Solution {
public int subtractProductAndSum(int n) {
int i=1;
int j=0;
int k=n;
while(k!=0){
i*=k%10;
j+=k%10;
k/=10;
}
return i-j;
}
}CSDN Community 《 Creative talent 》 Activities , As long as you participate in it and write articles, you will have the opportunity to win official prizes : Boutique calendar 、 New programmer magazine , Let's get involved ! Link directly to https://bbs.csdn.net/topics/605272551
边栏推荐
- Nutrecipes developed based on arkui ETS
- Pymongo fuzzy query
- Size limit display of pictures
- Cross domain and processing cross domain
- Save Xiaoyi from Netease written test -- a typical application of Manhattan distance
- Encountered 7 file(s) that should have been pointers, but weren‘t
- 如何注册码云账号
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
- async/await的执行顺序以及宏任务和微任务
- Storage and computing engine
猜你喜欢

Pytorch custom CUDA operator tutorial and runtime analysis

8 kinds of visual transformer finishing (Part 1)

js call和apply

500报错

一些实用、常用、效率越来越高的 Kubernetes 别名

Data interaction based on restful pages
![[I2C reading mpu6050 of Renesas ra6m4 development board]](/img/1b/c991dd0d798edbb7410a1e16f3a323.png)
[I2C reading mpu6050 of Renesas ra6m4 development board]

async/await的执行顺序以及宏任务和微任务

Cross domain and processing cross domain

音乐体验天花板!14个网易云音乐的情感化设计细节
随机推荐
Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry
"Weilai Cup" 2022 Niuke summer multi school training camp 1
openharmony萌新贡献指南
Some practical, commonly used and increasingly efficient kubernetes aliases
Principle of flex:1
The execution sequence of async/await, macro tasks and micro tasks
【进程间通信IPC】- 信号量的学习
Ctfshow ultimate assessment
How to study happily on station B?
< script> detailed explanation of label content
基于ArkUI eTS开发的坚果食谱(NutRecipes
500报错
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
Linux Installation and remote connection MySQL records
Is the operation of assigning values to int variables atomic?
Flex layout (actual Xiaomi official website)
ES6 new - function part
全排列递归思路整理
Two tips in arkui framework
Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support