【力扣刷题】整数除法

【力扣刷题】整数除法,第1张

【力扣刷题】整数除法

救命,人生第一次通过呜呜呜
整体利用减法思路~
1)a=0的情况—i=0;
2)a=-2147483648的情况—
①b=-1(因为会溢出)—i=2147483647
②b<0并且!=-1
如果a与b相等就为1,
不等:先判断2b是否溢出,不溢出就用a+|2b|<=0作为条件,true那就a=a+|2b|,i=i+2;
接着 a+|b|<=0作为条件,true那就a=a+|b|,i=i+1;(这里其实b不用判断是否溢出,因为b就是输入,谁会输入一个溢出的数呢,笑死,被自己整笑了)
(其实这两步就是脱裤子放屁,因为怕超出时间限制,这是为了当a很大,b很小的时候减少循环次数的)
③b>0是同样的作法
与上面不一样的就是,最后结果要取相反数
3)除掉以上的特殊情况之外,一般情况
①同号
同上2.2
②异号
同上2.3

class Solution {
    public int divide(int a, int b) {
        int i;
       i=0;
        if(a==0)
        {
            i=0;
        }
        else if(a==-2147483648 )
        { 
            if(b==-1)
            {
                 i=2147483647;
            }
            else if(b<0&&b!=-1)
            {
               i=0;
               if(a==b)
                 {
                     i=1;
                 }
               else
               {
                if (Math.abs(b) > Integer.MAX_VALUE /2|| Math.abs(b) < Integer.MIN_VALUE / 2)
               {
               }
               else
               {
               while(a+2*Math.abs(b)<=0)
               {
                     a=a+2*Math.abs(b);
                     i=i+2;
                }
                }
                while(a+Math.abs(b)<=0)
                {
                     a=a+1*Math.abs(b);
                     i=i+1;
                }
                }
            }
            else
            {
                i=0; 
               if (Math.abs(b) > Integer.MAX_VALUE /2|| Math.abs(b) < Integer.MIN_VALUE / 2)
                {
                }
                else
                {
                while(a<=-2*b)
                {
                     a=a+2*b;
                     i=i+2;
                }
                }
                 
                while(a<=-1*b)
                {
                     a=a+b;
                     i=i+1;
                }
                i=-i;
            }
        }
        else 
        {
            if (((a ^ b) >>> 31) == 0)
            {   
               if(Math.abs(a)==Math.abs(b))
                 {
                     i=1;
                 }
                 else if((((a-b) ^ a) >>> 31) == 1)
                 {
                   i=0;
                 }
                 else
                {  
                i=0;
                 if (Math.abs(b) > Integer.MAX_VALUE /5 || Math.abs(b) < Integer.MIN_VALUE / 5)
                {
                }
                else
                {
                     while(Math.abs(a)>=5*Math.abs(b))
                {
                     a=Math.abs(a)-5*Math.abs(b);
                     i=i+5;
                }
                }
                   if (Math.abs(b) > Integer.MAX_VALUE /4 || Math.abs(b) < Integer.MIN_VALUE / 4)
                {
                }
                else
                {
                     while(Math.abs(a)>=4*Math.abs(b))
                {
                     a=Math.abs(a)-4*Math.abs(b);
                     i=i+4;
                }
                }
                   if (Math.abs(b) > Integer.MAX_VALUE /3 || Math.abs(b) < Integer.MIN_VALUE / 3)
                {
                }
                else
                {
                     while(Math.abs(a)>=3*Math.abs(b))
                {
                     a=Math.abs(a)-3*Math.abs(b);
                     i=i+3;
                }
                }

                  if (Math.abs(b) > Integer.MAX_VALUE /2 || Math.abs(b) < Integer.MIN_VALUE /2)
                {
                }
                else
                {
                     while(Math.abs(a)>=2*Math.abs(b))
                {
                     a=Math.abs(a)-2*Math.abs(b);
                     i=i+2;
                }
                }

                    if (Math.abs(b) > Integer.MAX_VALUE /1 || Math.abs(b) < Integer.MIN_VALUE / 1)
                {
                
                }
                else
                {
                     while(Math.abs(a)>=Math.abs(b))
                {
                     a=Math.abs(a)-Math.abs(b);
                     i=i+1;
                }
                }
              }
            }
              else
            {  
                i=0; 
               if(Math.abs(a)==Math.abs(b))
                {
                    i=1;
                 }
                 else if((((a+b) ^ a) >>> 31) == 1)
                 {
                   i=0;
                 }
                 else
                 {   
                if (Math.abs(b) > Integer.MAX_VALUE /5 || Math.abs(b) < Integer.MIN_VALUE / 5)
                {
                }
                else
                {
                while(Math.abs(a)>=5*Math.abs(b))
                {
                     a=Math.abs(a)-5*Math.abs(b);
                     i=i+5;
                }
                }
                
                  if (Math.abs(b) > Integer.MAX_VALUE /4 || Math.abs(b) < Integer.MIN_VALUE / 4)
                {
                
                }
                else
                {
                     while(Math.abs(a)>=4*Math.abs(b))
                {
                     a=Math.abs(a)-4*Math.abs(b);
                     i=i+4;
                }
                }
                   if (Math.abs(b) > Integer.MAX_VALUE /3 || Math.abs(b) < Integer.MIN_VALUE /3)
                {
                
                }
                else
                {
                     while(Math.abs(a)>=3*Math.abs(b))
                {
                     a=Math.abs(a)-3*Math.abs(b);
                     i=i+3;
                }
                }

                   if (Math.abs(b) > Integer.MAX_VALUE /2 || Math.abs(b) < Integer.MIN_VALUE / 2)
                {
                
                }
                else
                {
                     while(Math.abs(a)>=2*Math.abs(b))
                {
                     a=Math.abs(a)-2*Math.abs(b);
                     i=i+2;
                }
                }

                   if (Math.abs(b) > Integer.MAX_VALUE /1 || Math.abs(b) < Integer.MIN_VALUE /1)
                {
                
                }
                else
                {
                     while(Math.abs(a)>=Math.abs(b))
                {
                     a=Math.abs(a)-Math.abs(b);
                     i=i+1;
                }
                }
                 }
                i=-i;
               // return -i;
            }
        }
    return i;
    }
}

救命,看了一下评论区,厉害的姐妹好多
贴个评论区看到的,转自力扣
思想差不多,但是明显我位运算不如人家啊哈哈哈
Integer.MIN_VALUE的绝对值还是他本身
Integer.MIN_VALUE的绝对值-5=+(2147483648-5)

 public int divide(int a, int b) {
	        if (a == 0) return 0;
	        if (b == 1) return a;
	        if (b == -1) return a == Integer.MIN_VALUE ? Integer.MAX_VALUE : -a;
	        if (b == 2) return a >> 1;
	        if (b == -2) return -(a >> 1);
	        int result = 0;
	        int tempa = Math.abs(a);
	        int tempb = Math.abs(b);
	        int temp = tempb;
	        // 如果使用下面的方法,则会出现超时的情况	
//	        while((temp += tempb) >= tempa){
//	            result ++;
//	        }
	        while((tempa -= tempb) >= 0){
	            result ++;
	        }
	        if((a < 0 && b < 0) || (a > 0 && b > 0)){
	            return result;
	        }else{
	            return -result;
	        }
	    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5692768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存