当前位置:网站首页>[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
边栏推荐
- [daily algorithm 94] classic interview question: motion range of robot
- QDoubleValidator不生效问题解决办法
- NPM and yarn update dependent packages
- 网易笔试之解救小易——曼哈顿距离的典型应用
- 存储和计算引擎
- Antdesign a-modal自定义指令实现拖拽放大缩小
- Huawei machine test question: String transformation minimum string JS
- 500 error reporting
- How to upload dynamic GIF map in blog
- D3.v3.js data visualization -- pictures and tips of force oriented diagram
猜你喜欢

Explanation of common basic controls for C # form application (suitable for Mengxin)

CUDA programming-05: flows and events

ES6 new - object part

Activation functions commonly used in deep learning

Pytorch custom CUDA operator tutorial and runtime analysis

Flex layout (actual Xiaomi official website)

Restful

500报错

Deep understanding of Kalman filter (3): multidimensional Kalman filter

音乐体验天花板!14个网易云音乐的情感化设计细节
随机推荐
Restful
Cross domain and processing cross domain
Summary of traversal methods
全排列递归思路整理
CUDA programming-01: build CUDA Programming Environment
npm和yarn 更新依赖包
Ctfshow ultimate assessment
MySQL basic knowledge learning (I)
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
The lifecycle of arkui development framework components
【进程间通信IPC】- 信号量的学习
Test picture
flex:1的原理
Pytorch custom CUDA operator tutorial and runtime analysis
New year's goals! The code is more standardized!
【每日算法Day 94】经典面试题:机器人的运动范围
npm install报错 强制安装
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
Deep understanding of Kalman filter (2): one dimensional Kalman filter
500报错