lintcode-medium-Divide Two Integers
Posted 哥布林工程师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-medium-Divide Two Integers相关的知识,希望对你有一定的参考价值。
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return 2147483647
public class Solution { /** * @param dividend the dividend * @param divisor the divisor * @return the result */ public int divide(int dividend, int divisor) { // Write your code here if(divisor == 0) return 2147483647; if(dividend == 0) return 0; if(divisor == 1) return dividend; if (dividend == Integer.MIN_VALUE && divisor == -1) { return Integer.MAX_VALUE; } boolean neg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0); long num = Math.abs((long) dividend); long den = Math.abs((long) divisor); long result = 0; while(num >= den){ int shift = 0; while(num >= (den << shift)) shift++; num -= den<<(shift - 1); result += 1 << (shift - 1); } if(neg){ if(result > Math.abs((long) Integer.MIN_VALUE)) return 2147483647; else return (int) (-result); } else{ if(result > (long) Integer.MAX_VALUE) return 2147483647; else return (int) result; } } }
以上是关于lintcode-medium-Divide Two Integers的主要内容,如果未能解决你的问题,请参考以下文章
Linux下TIME_WAIT连接优化内核参数tcp_tw_reuse与tcp_tw_recycle区别与联系浅析
ASEMI代理TW6869-TA1-CRH瑞萨TW6869-TA1-CRH车规级
linux内核网络参数tcp_tw_recycle 和 tcp_tw_reuse 你搞清楚了吗?