当前位置:网站首页>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;
}
}
边栏推荐
- 剑指offer基础版 ---- 第27天
- Unity Framework Design Series: How Unity Designs Network Frameworks
- 【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
- a different object with the same identifier value was already associated with the session
- 工作流编排引擎-Temporal
- C语言的文件操作(一)
- 质量小议12 -- 以测代评
- mysql存储过程
- Element concatenation operations in numpy and pytorch: stack, concatenat, cat
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
猜你喜欢

剑指offer基础版 --- 第22天

mysql uses on duplicate key update to update data in batches

MySQL transaction isolation level, rounding

太厉害了,终于有人能把文件上传漏洞讲的明明白白了

剑指offer基础版--- 第23天

MySQL transaction (transaction) (this is enough..)

MySQL optimization slow log query

Unity Framework Design Series: How Unity Designs Network Frameworks

Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric

Temporal介绍
随机推荐
剑指offer基础版 --- 第21天
剑指offer基础版 ----- 第28天
Simple read operation of EasyExcel
The interviewer asked me TCP three handshake and four wave, I really
DVWA installation tutorial (understand what you don't understand · in detail)
mysql 的简单运用命令
MYSQL下载及安装完整教程
mysql stored procedure
质量小议12 -- 以测代评
【MQ我可以讲一个小时】
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
Mysql——字符串函数
With MVC, why DDD?
ES source code API call link source code analysis
剑指offer专项突击版 ---- 第2天
C语言教程(一)-准备
剑指offer专项突击版 --- 第 3 天
About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Pytorch教程Introduction中的神经网络实现示例
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?