当前位置:网站首页>剑指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;
}
}
边栏推荐
猜你喜欢
随机推荐
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
MYSQL一站式学习,看完即学完
ES source code API call link source code analysis
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
【LeetCode-SQL每日一练】——2. 第二高的薪水
质量小议12 -- 以测代评
DVWA靶场环境搭建
MySQL_关于JSON数据的查询
mysql存储过程
Flink sink redis 写入Redis
面试官,不要再问我三次握手和四次挥手
mysql stored procedure
Shell重油常压塔模拟仿真与控制
Anaconda配置环境指令
限流的原理
CentOS7 安装MySQL 图文详细教程
面试官问我TCP三次握手和四次挥手,我真的是
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
【一起学Rust】Rust学习前准备——注释和格式化输出