当前位置:网站首页>剑指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;
}
}
边栏推荐
- 分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
- centos7安装mysql5.7
- STM32——DMA
- MYSQL一站式学习,看完即学完
- Go中间件
- MySQL database installation (detailed)
- 面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
- mysql stored procedure
- The 15th day of the special assault version of the sword offer
- What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
猜你喜欢
【一起学Rust】Rust学习前准备——注释和格式化输出
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
MySQL-如何分库分表?一看就懂
如何将项目部署到服务器上(全套教程)
centos7安装mysql5.7步骤(图解版)
Multiple table query of sql statement
MYSQL下载及安装完整教程
DVWA之SQL注入
基于web3.0使用钱包Metamask的三方登陆
Mysql应用安装后找不到my.ini文件
随机推荐
MySQL (updating)
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
Mysql应用安装后找不到my.ini文件
[Cloud Native] DevOps (5): Integrating Harbor
Unity Framework Design Series: How Unity Designs Network Frameworks
MySQL transaction isolation level, rounding
MySQL optimization slow log query
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
1. Get data - requests.get()
Flink sink ES 写入 ES(带密码)
STM32——DMA
ES 源码 API调用链路源码分析
mysql存储过程
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
CentOS7 install MySQL graphic detailed tutorial
MYSQL下载及安装完整教程
Redis的初识
Typec手机有线网卡网线转网口转接口快充方案
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
Reference code series_1. Hello World in various languages