当前位置:网站首页>Sword Point Offer Special Assault Edition ---- Day 1
Sword Point Offer Special Assault Edition ---- Day 1
2022-07-31 05:32:00 【Milan's little red and black】
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;
}
}
边栏推荐
- STM32——DMA
- With MVC, why DDD?
- mysql5.7.35安装配置教程【超级详细安装教程】
- 剑指offer基础版 ---- 第29天
- SQL statement to range query time field
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- Temporal线上部署
- Distributed transaction processing solution big PK!
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
猜你喜欢
目标检测学习笔记
MySQL window function
MySQL (updating)
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
剑指offer专项突击版 ---- 第2天
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
MySQL forgot password
剑指offer专项突击版 --- 第 3 天
CentOS7 —— yum安装mysql
Mysql——字符串函数
随机推荐
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Anaconda配置环境指令
【LeetCode-SQL每日一练】——2. 第二高的薪水
Apache DButils使用注意事项--with modifiers “public“
基于flask的三方登陆的流程
限流的原理
MySQL_关于JSON数据的查询
C语言教程(一)-准备
剑指offer基础版 ----- 第25天
[Detailed explanation of ORACLE Explain]
Temporal线上部署
剑指offer基础版 --- 第24天
剑指offer基础版 ---- 第26天
Distributed transaction processing solution big PK!
Flink sink redis 写入Redis
MySQL (updating)
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
[MQ I can speak for an hour]