string removeKdigits(string num, int k) {
     if (num.size() <= k || num.size() == 0) return "0";
     // stack is used to preserve a increasing number vector.
     // consider element order in stack
     stack<int> s;
     int counter = 0, l = 0;
     while (l < num.size()) {
         if (s.empty() || num[s.top()] <= num[l]) { // remove local max
             s.push(l++);
         } else {
             num.erase(--l, 1);
             s.pop();
             counter++;
             if (counter == k) break;
         } 
     }
     if (counter == k) return removeLeadingZeros(num);
     else return removeLeadingZeros(num.substr(0, num.size() - (k - counter)));
 }
Example #2
0
void removeLeadingZeros(ListNode* head) {
    if (!head || !head->next) return;
    removeLeadingZeros(head->next);
    if (head->val != '-' && head->val != '+' && head->next->val == '0' && head->next->next == NULL) {
        delete head->next;
        head->next = NULL;
    }
    if (head->val == '-' && head->next->val == '0' && head->next->next == NULL)
        head->val = '+';
    return;
}
	BigInteger& operator /= (int rhs) {
		long long divider = rhs; //avoid overflow with MIN_INT
		if (divider == 0)
			throw VerdictException(Verdict::FAIL, "division by zero");
		if (divider < 0)
			sign = -sign, divider = -divider;
		for (size_t i = data.size(), rem = 0; i-- > 0; ) {
			long long cur = data[i] + rem * (long long) base;
			data[i] = (int) (cur / divider);
			rem = (int)(cur % divider);
		}
		removeLeadingZeros();
		return *this;
	}
	BigInteger& operator *= (int rhs) {
		long long multiplier = rhs; //avoid overflow with MIN_INT
		if (multiplier < 0) {
			sign = -sign;
			multiplier = -multiplier;
		}
		for (size_t i = 0, carry = 0; i < data.size() || carry; ++i) {
			if (i == data.size())
				data.push_back(0);
			long long cur = data[i] * (long long) multiplier + carry;
			carry = (size_t) (cur / base);
			data[i] = (int) (cur % base);
		}
		removeLeadingZeros();
		return *this;
	}
	BigInteger& operator -= (const BigInteger &deduction) {
		if (sign == deduction.sign || deduction.isZero()) {
			if (abs() >= deduction.abs()) {
				for (size_t i = 0, carry = 0; i < deduction.data.size() || carry; ++i) {
					data[i] -= carry + (i < deduction.data.size() ? deduction.data[i] : 0);
					carry = data[i] < 0;
					if (carry)
						data[i] += base;
				}
				removeLeadingZeros();
				return *this;
			}
			*this = -(deduction - *this);
			return *this;
		}
		*this += (-deduction);
		return *this;
	}
	explicit BigInteger(const std::string &s) {
		sign = 1;
		size_t pos = 0;
		while (pos < s.size() && (s[pos] == '-' || s[pos] == '+')) {
			if (s[pos] == '-')
				sign = -sign;
			++pos;
		}
		if(pos > 1)
			throw VerdictException(Verdict::FAIL, "Too many signs");
		for (size_t i = s.size(); i > pos;) {
			int x = 0;
			size_t startIndex = i > pos + baseDigits ? i - baseDigits : pos;//max, avoid underflow
			for (size_t j = startIndex; j < i; ++j)
				x = x * 10 + s[j] - '0';
			data.push_back(x);
			i = startIndex;
		}
		removeLeadingZeros();
	}
Example #7
0
void InfInt::correct(bool justCheckLeadingZeros, bool hasValidSign)
{//PROFILED_SCOPE
	if (!justCheckLeadingZeros)
	{
		truncateToBase();

		if (equalizeSigns())
		{
			pos = ((val.size() == 1 && val[0] == 0) || !hasValidSign) ? true : pos;
		}
		else
		{
			pos = hasValidSign ? !pos : false;
			for (size_t i = 0; i < val.size(); ++i)
			{
				val[i] = abs(val[i]);
			}
		}
	}

	removeLeadingZeros();
}
Example #8
0
ListNode* AddLists(ListNode* l1, ListNode* l2) {
    if (!l1 && !l2) return NULL;
    else if (!l1) return l2;
    else if (!l2) return l1;
    if (larger(l2, l1)) swap(l1, l2);
    bool sign = true;
    int plus = 1;
    if (l1->val == '-') { sign = false; plus = -plus; }
    if (l2->val == '-') { plus = -plus;} 
    l1 = l1->next;
    l2 = l2->next;
    ListNode* head = NULL;
    ListNode* prev = NULL;
    int carry = 0;
    while (l1 || l2) {
        int tmp=l1->val-'0'+carry+ plus*((l2) ? l2->val-'0' : 0);
        if (tmp < 0) {
            carry = -1;
            tmp = tmp+10;
        } else { 
            carry = tmp/10; 
            tmp = tmp%10;
        }
        ListNode* cur= new ListNode(tmp+'0');
        if (!prev) head = cur; 
        else prev->next = cur;
        prev = cur;
        l1 = l1->next; 
        if (l2) l2 = l2->next;
    }
    assert(carry >= 0);
    if (carry) prev->next = new ListNode('1');
    ListNode* s = new ListNode(((sign) ? '+': '-'));
    s->next = head;
    head = s;
    removeLeadingZeros(head);
    return head;  
}