Programmer Interview Gold Code-Interview Question 16.09. Operation (only multiplication and division by + method)

1. Title

Please implement multiplication, subtraction and division of integer numbers. The operation results are all integer numbers.
Only addition operators and logical operators are allowed in the program. Positive and negative constants are allowed in the program. Bit operations are not allowed.

Your implementation should support the following operations:

  • Operations () constructor
  • minus (a, b) subtraction, returns a-b
  • multiply (a, b) Multiply, return a * b
  • divide (a, b) division, return a / b
示例:
Operations operations = new Operations();
operations.minus(1, 2); //返回-1
operations.multiply(3, 4); //返回12
operations.divide(5, -2); //返回-2

提示:
你可以假设函数输入一定是有效的,例如不会出现除法分母为0的情况
单个用例的函数调用次数不会超过1000

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/operations-lcci The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

2. Problem solving

Reference problem solution : the multiplier and divisor 2 n 2^n Store the result of times, continuously add or subtract

class Operations {
public:
    Operations() {

    }
    
    int minus(int a, int b) {
    	return a+(-b);
    }
    
    int multiply(int a, int b) {
    	if(a==0 || b==0)	return 0;
    	if(a==1)	return b;
    	if(b==1)	return a;
    	if(a== -1)	return -b;
    	if(b== -1)	return -a;
    	int negative = 0;
    	if(a < 0) negative += 1, a = -a;
    	if(b < 0) negative += 1, b = -b;
    	if(a > b) swap(a,b);// b*a
    	long temp = b;
    	vector<int> b_2, count;
    	int plus = 1;
    	while(temp <= INT_MAX)//b_2数组[b*1,b*2,b*4...]
    	{	
    		b_2.push_back(temp);
    		count.push_back(plus);//b乘以几得到上面的数temp
    		temp += temp;
    		plus += plus;
    	}
    	int ans = 0;
    	for(int i = b_2.size()-1; i >= 0; i=minus(i,1))
    	{
    		while(a >= count[i])
    		{
    			ans += b_2[i];
    			a = minus(a,count[i]);//把a拆分
    		}
    	}
    	if(negative==1)
    		return -ans;
    	return ans;
    }
    
    int divide(int a, int b) {
        if(a==0 || b==INT_MAX || b==INT_MIN) return 0;
        if(b==1) return a;
        if(b== -1) return -a;
        int negative = 0;
    	if(a < 0) negative += 1, a = -a;
    	if(b < 0) negative += 1, b = -b;
    	if(a < b) return 0;
    	long temp = b;
    	vector<int> b_2, count;
    	int plus = 1;
    	while(a >= temp)
    	{	
    		b_2.push_back(temp);//[b*1,b*2,b*4,...]
    		count.push_back(plus);//b乘以几得到上面的数temp
    		temp += temp;
    		plus += plus;
    	}
    	int ans = 0;
    	for(int i = b_2.size()-1; i >= 0; i=minus(i,1))
    	{
    		while(a >= b_2[i])
    		{
    			ans += count[i];//商+倍数
    			a = minus(a,b_2[i]);//把a减去b的倍数
    		}
    	}
    	if(negative==1)
    		return -ans;
    	return ans;
    }
};

84 ms 15 MB

Published 830 original articles · Like 1993 · Visit 440,000+

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105451309