当前位置:网站首页>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;
}
}
边栏推荐
- MYSQL一站式学习,看完即学完
- MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
- 剑指offer基础版 --- 第21天
- 账号或密码多次输入错误,进行账号封禁
- Flink sink ES 写入 ES(带密码)
- STM32 - DMA
- mysql uses on duplicate key update to update data in batches
- 关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
- 【LeetCode-SQL每日一练】——2. 第二高的薪水
- Quickly master concurrent programming --- the basics
猜你喜欢
Unity Framework Design Series: How Unity Designs Network Frameworks
有了MVC,为什么还要DDD?
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
Mysql——字符串函数
mysql5.7.35安装配置教程【超级详细安装教程】
剑指offer基础版 ----第31天
[mysql improves query efficiency] Mysql database query is slow to solve the problem
剑指offer专项突击版 --- 第 3 天
剑指offer基础版 --- 第24天
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
随机推荐
Unity resources management series: Unity framework how to resource management
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
[Detailed explanation of ORACLE Explain]
DVWA shooting range environment construction
Data set partitioning and cross-validation
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
数据库上机实验5 数据库安全性
分布式事务处理方案大 PK!
剑指offer基础版 --- 第24天
Typec手机有线网卡网线转网口转接口快充方案
MySQL transaction isolation level, rounding
C语言教程(二)-printf及c自带的数据类型
Anaconda configure environment directives
数据库学习笔记
STM32——DMA
基于web3.0使用钱包Metamask的三方登陆
一文了解大厂的DDD领域驱动设计
docker安装postgresSQL和设置自定义数据目录
Centos7 install mysql5.7 steps (graphical version)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍