当前位置:网站首页>剑指offer专项突击版 ---- 第1天
剑指offer专项突击版 ---- 第1天
2022-07-31 05:09:00 【米兰的小红黑】
class Solution {
public int divide(int a, int b) {
if(a == Integer.MIN_VALUE && b == -1){
return Integer.MAX_VALUE;
}
boolean flag = false;
if((a>0 && b > 0) || (a < 0 && b < 0)){
flag = true;
}
if(a > 0){
a = -a;
}
if(b > 0){
b = -b;
}
int res = 0;
while(a <= b){
a = a - b;
res++;
}
return flag == true ? res : -res;
}
}
class Solution {
public String addBinary(String a, String b) {
StringBuffer ans = new StringBuffer();
int n = Math.max(a.length(), b.length()), carry = 0;
for (int i = 0; i < n; ++i) {
carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
ans.append((char) (carry % 2 + '0'));
carry /= 2;
}
if (carry > 0) {
ans.append('1');
}
ans.reverse();
return ans.toString();
}
}
class Solution {
public int[] countBits(int n) {
int[] arr = new int[n+1];
for(int i = 0; i <= n; i++){
int mid = i;
int j = 0;
while(mid > 0){
if((mid & 1) == 1){
j++;
}
mid = mid >> 1;
}
arr[i] = j;
}
return arr;
}
}
边栏推荐
猜你喜欢
随机推荐
Flink sink redis 写入Redis
MySQL开窗函数
SQL语句中对时间字段进行区间查询
Minio上传文件ssl证书不受信任
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
限流的原理
关于superset集成到自己的项目中
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
MySQL忘记密码怎么办
12个MySQL慢查询的原因分析
MySQL optimization slow log query
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
【LeetCode-SQL每日一练】——2. 第二高的薪水
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
Lock wait timeout exceeded解决方案
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
Why use Flink and how to get started with Flink?
Linux系统安装mysql(rpm方式安装)
Mysql——字符串函数
面试官问我TCP三次握手和四次挥手,我真的是