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;
}
}

最新文章

  1. APP开发+发布流程
  2. jquery选择器(总结)
  3. 一些随机数据的生成(日期,邮箱,名字,URL,手机号,日期等等)
  4. [DPDK][转]DPDK编程开发(4)—lcore
  5. vs2015企业版太大了
  6. C#的Enum——枚举
  7. c++实现des算法
  8. 第三章 Models模块属性详解
  9. HDU 2159 FATE(二维费用背包)
  10. 【HDU2087】KMP
  11. 小三角图标如何用CSS写
  12. 10:Hello, World!的大小
  13. LoadRunner利用ODBC编写MySql脚本
  14. ThinkPHP使用Memcached缓存数据
  15. Android在第三方应用程序系统应用尽早开始,杀死自己主动的第三方应用程序,以重新启动
  16. 从 HelloWorld 看 Java 字节码文件结构
  17. 设计模式 --&gt; (4)建造者模式
  18. 利用SHA-1算法和RSA秘钥进行签名验签(带注释)
  19. php的phar是什么?
  20. python之路--基础数据类型的补充与深浅copy

热门文章

  1. Git实战指南----跟着haibiscuit学Git(第八篇)
  2. arcgis api 4.x for js 结合 Echarts4 实现统计图(附源码下载)
  3. 《Python3 网络爬虫开发实战》开发环境配置过程中踩过的坑
  4. V4 Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 2471245.1)
  5. linux中的交换分区(swap)及优化
  6. 【学习笔记】《Java编程思想》 第1~7章
  7. 关于web.xml配置的那些事儿
  8. [CodeForces-1225A] Forgetting Things 【构造】
  9. 2019阿里天猫团队Java高级工程师面试题之第一面
  10. Ubuntu18.04 下最好用的gif录制工具peek