当前位置:网站首页>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;
}
}
边栏推荐
- Paginate the list collection and display the data on the page
- Unity mobile game performance optimization series: performance tuning for the CPU side
- mysql存储过程
- MySQL (updating)
- Data set partitioning and cross-validation
- 剑指offer专项突击版 --- 第 4 天
- Summary of MySQL common interview questions (recommended collection!!!)
- Flink sink ES 写入 ES(带密码)
- 面试官问我TCP三次握手和四次挥手,我真的是
- MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
猜你喜欢
剑指offer基础版 ----- 第25天
剑指offer专项突击版 --- 第 4 天
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
一文了解大厂的DDD领域驱动设计
限流的原理
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
Anaconda configure environment directives
CentOS7 —— yum安装mysql
MySQL forgot password
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
随机推荐
C语言实验二 数据类型、运算符和表达式
为什么要用Flink,怎么入门使用Flink?
C语言实验四 循环结构程序设计(一)
数据库上机实验7 数据库设计
【一起学Rust】Rust学习前准备——注释和格式化输出
剑指offer专项突击版 --- 第 4 天
C语言指针详解
Swordsman Offer Special Assault Edition --- Day 3
Data set partitioning and cross-validation
对list集合进行分页,并将数据显示在页面中
Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
mysql stored procedure
C语言的文件操作(一)
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
剑指offer基础版 ---- 第29天
C语言实验五 循环结构程序设计(二)
docker安装postgresSQL和设置自定义数据目录
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
数据库上机实验5 数据库安全性