一、使用long类型
class Solution { public: long divide2(long dividend,long divisor){ if(dividend < 0 && divisor < 0) return divide2(-dividend,-divisor); else if(dividend < 0 && divisor > 0) return -divide2(-dividend,divisor); else if(dividend > 0 && divisor < 0) return -divide2(dividend,-divisor); if(divisor == 1) return dividend; if(dividend < divisor) return 0; long num = 0; while(dividend > 0){ dividend -= divisor; num++; } if(dividend < 0) return num-1; return num; } int divide(int dividend, int divisor) { long MAX = pow(2,31); long num = divide2(dividend,divisor); if(num > MAX-1) return MAX-1; else if(num < -MAX) return -MAX; return num; } };
用时和消耗内存表现特别差
标签:return,divisor,MAX,29,相除,num,long,dividend,leetcode From: https://www.cnblogs.com/uacs2024/p/18542294