<Math> 29 365
2024-10-18 21:29:31
29. Divide Two Integers
class Solution {
public int divide(int dividend, int divisor) {
if(dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
long m = (long)dividend, n = (long)divisor;
int sign = 1, res = 0;
if(m < 0){
m = -m;
sign = -sign;
}
if(n < 0){
n = -n;
sign = -sign;
}
while(m >= n){
int shift = 0;
while(m >= n << shift){
shift++;
}
res += (1 <<(shift - 1));
m -= (n <<(shift - 1));
}
return sign * res;
}
}
365. Water and Jug Problem
class Solution {
public boolean canMeasureWater(int x, int y, int z) {
//limit brought by the statement that water is finallly in one or both buckets
if(x + y < z) return false;
//case x or y is zero
if( x == z || y == z || x + y == z ) return true; //get GCD, then we can use the property of Bézout's identity
return z%GCD(x, y) == 0;
} public int GCD(int a, int b){
while(b != 0 ){
int temp = b;
b = a%b;
a = temp;
}
return a;
}
}
最新文章
- APP开发+发布流程
- jquery选择器(总结)
- 一些随机数据的生成(日期,邮箱,名字,URL,手机号,日期等等)
- [DPDK][转]DPDK编程开发(4)—lcore
- vs2015企业版太大了
- C#的Enum——枚举
- c++实现des算法
- 第三章 Models模块属性详解
- HDU 2159 FATE(二维费用背包)
- 【HDU2087】KMP
- 小三角图标如何用CSS写
- 10:Hello, World!的大小
- LoadRunner利用ODBC编写MySql脚本
- ThinkPHP使用Memcached缓存数据
- Android在第三方应用程序系统应用尽早开始,杀死自己主动的第三方应用程序,以重新启动
- 从 HelloWorld 看 Java 字节码文件结构
- 设计模式 -->; (4)建造者模式
- 利用SHA-1算法和RSA秘钥进行签名验签(带注释)
- php的phar是什么?
- python之路--基础数据类型的补充与深浅copy
热门文章
- Git实战指南----跟着haibiscuit学Git(第八篇)
- arcgis api 4.x for js 结合 Echarts4 实现统计图(附源码下载)
- 《Python3 网络爬虫开发实战》开发环境配置过程中踩过的坑
- V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245.1)
- linux中的交换分区(swap)及优化
- 【学习笔记】《Java编程思想》 第1~7章
- 关于web.xml配置的那些事儿
- [CodeForces-1225A] Forgetting Things 【构造】
- 2019阿里天猫团队Java高级工程师面试题之第一面
- Ubuntu18.04 下最好用的gif录制工具peek