< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Q1: Pow(x, n)
  • Q2: Multiply Strings

Was this helpful?

  1. Leetcode Programming Skill

Math

PreviousLeetcode Programming SkillNextLeetcode 75

Last updated 9 months ago

Was this helpful?

Q1:

class Solution {
public:
    double myPow(double x, long int n) {
        long int N;
        if (n < 0) {
            N = -(long)n;
            return 1.0 / sol(x, N);
        }
        return sol(x, n);
    }

    double sol(double x, long int n) {
        if(n == 0) return 1;
        else if(n == 1) return x;
        else if(n%4 == 0) {
            double y = sol(x, n/4);
            return y*y*y*y;
        }
        else if(n%2 == 0) {
            double y = sol(x, n/2);
            return y*y;
        }
        else {
            double y = sol(x, (n-1)/2);
            return y*y*x;
        }
     }
};
class Solution {
public:
    
    string multiply(string num1, string num2) {
        if(num1[0] == '0' || num2[0] == '0') return "0";

        string sum(num1.size() + num2.size(), '0');
    
        for (int i = num1.size() - 1; i >= 0; i--) {
            int carry = 0;
            for (int j = num2.size() - 1; j >= 0; j--) {
                int temp = (sum[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry;
                sum[i + j + 1] = temp % 10 + '0';
                carry = temp / 10;
            }
            sum[i] += carry;
        }
        
        int i = 0;
        while(sum[i] == '0') i++; // removing prefix zeroes
        
        string res = "";
        for(i; i < sum.size(); i++) res += sum[i];
        
        return res;
    }
};

Q2:

Pow(x, n)
Multiply Strings