Posts

Showing posts with the label Maths

Plus One

  Given a   non-empty   array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.   Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Example 3: Input: digits = [0] Output: [1]   Constraints: 1 <= digits.length <= 100 0 <= digits[i] <= 9 Solution: Algorithm: 1) Add 1 to the nth element in the array. If the sum is greater than 9 then take the ten's element (save it to 'carry' ) and move to the (n-1)th element.  2) Continue step 1 till we don't have a carry. Time Complexity:  O(n) Space Complexity: O(1)

Roman To Integer

Roman numerals are represented by seven different symbols:  I ,  V ,  X ,  L ,  C ,  D  and  M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as  II  in Roman numeral, just two one's added together. Twelve is written as,  XII , which is simply  X  +  II . The number twenty seven is written as  XXVII , which is  XX  +  V  +  II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to ...

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Could you solve it without converting the integer to a string?   Solution: One approach is to convert the number to string and then compare the string with it's reverse. Second approach which is also the answer to the follow-up question is to reverse the number and then compare the original and the reversed number. Time Complexity : O(log x) Space Complexity: O(1)

Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 ,  2 31  − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. Solution: Time Complexity: O(log x) Space Complexity: O(1)