Skip to main content

Posts

Leet Code Problem #537. Complex Number Multiplication

Given two strings representing two complex numbers . You need to return a string representing their multiplication. Note i 2 = -1 according to the definition. Example 1: Input: "1+1i", "1+1i" Output: "0+2i" Explanation: (1 + i) * (1 + i) = 1 + i 2 + 2 * i = 2i,  and you need convert it to the form of 0+2i.   Solution in C++:     string complexNumberMultiply(string a, string b) {         int a1 = 0;         int a2 = 0;         int b1 = 0;         int b2 = 0;         string ret;         cal(a, a1, a2);         cal(b, b1, b2);         int real = a1 * b1 - a2 * b2;         int imag = a1 * b2 + a2 * b1;      ...

Most frequently asked interview question #15 with solution

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10. This is how the UTF-8 encoding would work: Char. number range | UTF-8 octet sequence (hexadecimal) | (binary) --------------------+--------------------------------------------- 0000 0000-0000 007F | 0xxxxxxx 0000 0080-0000 07FF | 110xxxxx 10xxxxxx 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx Given an array of integers representing the data, return whether it is a valid utf-8 encoding. Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data. Example 1: dat...

Most frequently asked interview question #14 with solution

Note: This is a companion problem to the System Design problem: Design TinyURL . TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk . Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL. Solution in C++: class Solution { public:     map<string, string> url;         // Encodes a URL to a shortened URL.     string encode(string longUrl) {         static int count = 0;         char *a = (char*)calloc(sizeof(char), 30);         ++count;         spr...

Most frequently asked interview question #13 with solution

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Input: root = [10,5,15,3,7,null,18] , L = 7 , R = 15 Output: 32     Solution in C++: /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}  * };  */     int rangeSumBST(TreeNode* root, int L, int R) {             int sum = 0;         return cal_sum(root, L, R, sum);     }     int cal_sum (TreeNode* root, int L, int R, int &sum){         if(...

Most frequently asked interview question #12 with solution

Balanced  strings are those who have an equal quantity of 'L' and 'R' characters. Given a balanced string  s  split it in the maximum amount of balanced strings. Return the maximum amount of split balanced strings. Balanced  strings are those who have an equal quantity of 'L' and 'R' characters. Given a balanced string  s  split it in the maximum amount of balanced strings. Return the maximum amount of split balanced strings. Input: s = "RLRRLLRLRL" Output: 4 Solution in C++:     int balancedStringSplit(string s) {             int ret = 0;             int r_c = 0;             int l_c = 0;         for(int iter = 0; iter < s.length(); ++iter){               ...

Most frequently asked interview question #11 with solution

Given two arrays of integers  nums and index . Your task is to create target array under the following rules: Initially target array is empty. From left to right read nums[i] and index[i], insert at index index[i]  the value nums[i]  in  target array. Repeat the previous step until there are no elements to read in nums and index. Return the target array. It is guaranteed that the insertion operations will be valid. Input: nums = [0,1,2,3,4], index = [0,1,2,2,1] Output: [0,4,1,3,2] Explanation: nums index target 0 0 [0] 1 1 [0,1] 2 2 [0,1,2] 3 2 [0,1,3,2] 4 1 [0,4,1,3,2]   Solution in C++:   vector<int> createTargetArray(vector<int>& nums, vector<int>& index) { int *a = nullptr; int size = nums.size(); a = (int*)calloc(sizeof(int), size); a[0] = nums.at(0); ...

Most frequently asked interview question #10 with solution

We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]]  (with i >= 0 ).  For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list. Return the decompressed list. Input: nums = [1,2,3,4] Output: [2,4,4,4]   Solution in C++:   vector<int> decompressRLElist(vector<int>& nums) { vector<int> ret; for(int iter = 0; iter < nums.size();){ int i = 0; while(i < nums.at(iter)){ ++i; ret.push_back(nums.at(iter + 1)); } iter += 2; } return ret; }