当前位置:网站首页>牛客网刷题——运算符问题
牛客网刷题——运算符问题
2022-07-30 16:26:00 【命由己造~】
一、不用加减乘除做加法
题目描述:
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
数据范围:两个数都满足 -10 \le n \le 1000−10≤n≤1000
进阶:空间复杂度 O(1)O(1),时间复杂度 O(1)O(1)
示例:
示例1
输入:1,2
返回值:3
示例2
输入:0,0
返回值:0
分析思路:
题目不让用四则运算符号,所以我们需要用位运算来达到加法的目的。
位运算的本质是对二进制位操作,通过找规律可以发现:
位运算中两数进行^(异或运算)可以提供两数加和后二进制非进位信息,位运算中的两数进行&(与运算)的结果可以提供两数加和后的二进制进位信息
如图:
通过观察可以发现:如果在没有进位的情况下两个二进制序列^后就为结果sum,在加上进位信息就是有进位的情况。所以当进位信息不为0时,我们就可以先左移进位信息^后得到非进制信息,再&看进位信息是否为0,如此循环。
int Add(int num1, int num2 ) {
// write code here
int add = num2;
int sum = num1;
while(add != 0)
{
int tmp = add ^ sum;
//进位信息
add = (sum & add) << 1;
sum = tmp;
}
return sum;
}
二、二进制中1的个数
题目描述:
输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。
数据范围:- 2^31 <= n <= -2^31-1−2^31<=n<=2^31−1
即范围为:-2147483648<= n <= 2147483647−2147483648<=n<=2147483647
示例一:
输入:10
返回值:2
说明:
十进制中10的32位二进制表示为0000 0000 0000 0000 0000 0000 0000 1010,其中有两个1。
示例二:
输入:-1
返回值:32
说明:
负数使用补码表示 ,-1的32位二进制表示为1111 1111 1111 1111 1111 1111 1111 1111,其中32个1
思路分析:
因为因负数用补码表示,故不能用连除法。
我们可以让目标数字右移&数字1,如果结果为1,就说明最后一位是1,就可以看到有多少位1
int NumberOf1(int n ) {
int count = 0;
int i = 0;
for(i = 0; i < 32; i++)
{
if((n >> i) & 1 == 1)
count++;
}
return count;
}
三、有限制求1+2+3+…+n
题目描述:
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围: 0 < n \le 2000<n≤200
进阶: 空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n)
示例一:
输入:5
返回值:15
示例二:
输入:1
返回值:1
思路分析:
不让用乘除法和条件判断语句就说明不能使用公式法和循环。
这很容易就能想到递归:
一般递归是这么实现:
int Sum_Solution(int n)
{
if (n == 1)
{
return 1;
}
return n + Sum_Solution(n - 1);
}
但是这里还是会用到条件判断语句,那我们就可以巧用 && 的短路性质,如果&&的左边为假,右边就不会进行:
int Sum_Solution(int n ) {
// write code here
n && (n += Sum_Solution(n - 1));
return n;
}
边栏推荐
- The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.
- How to remove first character from php string
- go 学习03 基础知识变量类型
- 配置Path环境变量
- 新技术要去做新价值
- 23. Please talk about the difference between IO synchronization, asynchronous, blocking and non-blocking
- 基于STM32F407使用ADC采集电压实验
- Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
- 在 Chrome 浏览器中安装 JSON 显示插件
- The case of five little pigs (five little pigs compare the size of the body weight)
猜你喜欢

Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?

How does the new retail saas applet explore the way to break the digital store?

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

安全业务收入增速超70% 三六零筑牢数字安全龙头

云风:不加班、不炫技,把复杂的问题简单化

The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.

hcip--ospf综合实验

DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design

PCIE入门

Leetcode 118. 杨辉三角
随机推荐
[AGC] Quality Service 2 - Performance Management Example
PyQt5快速开发与实战 9.2 数据库处理
安全业务收入增速超70% 三六零筑牢数字安全龙头
@Bean注解详解
Wuhan Star Sets Sail: Overseas warehouse infrastructure has become a major tool for cross-border e-commerce companies to go overseas
lotus 1.16.0 最小快照导出 导入
node.js中怎么连接redis?
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
Jetpack Compose 到底优秀在哪里?| 开发者说·DTalk
Redis 复习计划 - Redis 数据结构和持久化机制
vivo announced to extend the product warranty period, the system launched a variety of functional services
Leetcode 119. Yang Hui's Triangle II
win下搭建php环境的方法
rhce笔记3
登录模块调试-软件调试入门
23. 请你谈谈关于IO同步、异步、阻塞、非阻塞的区别
Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?
Goland opens file saving and automatically formats
支付系统架构设计详解,精彩!
rscsa笔记八