C# one level loop x 2. straightforward


  • 0
    H
    public class Solution {
        public int Divide(int dividend, int divisor) {
    		long de = Math.Abs((long)dividend);
    		long dr = Math.Abs((long)divisor);
    
    		long trial = dr;
    		long unsignedans = 1;
    		while (de - trial > 0) {
    			trial = trial << 1;
    			unsignedans = unsignedans << 1;
    		}
    
    		while (de - trial < 0) {
    			trial -= dr;
    			unsignedans--;
    		}
    		
    		var ans = dividend > 0 ^ divisor > 0 ? -unsignedans : unsignedans;
    		if (ans > int.MaxValue) return int.MaxValue;
    		return (int) ans;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.