We are familiar base \(10\). How do we calculate a number?
From high to low, starting with \(ans\) as \(0\), update \(ans\) with the current digit value each time, the update rule is \(ans = ans \times 10 + val\).
If there is a decimal number, encoded as \(\textit{ABC}\) not the arabic number, what’s it?
1 2 3 4
ans = 0 ans = ans * 10 + 1 => A ans = ans * 10 + 2 => B ans = ans * 10 + 3 => C
The answer is: \(123\).
from High to Low
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicinttitleToNumber(String columnTitle) { intn= columnTitle.length(); intans=0; intbase=1; for (inti= n - 1; i >= 0; i--) { inttemp=0; if (columnTitle.charAt(i) == 'Z') { temp = 26; } else { temp = columnTitle.charAt(i) - 'A' + 1; } ans = ans + base * temp; base *= 26; }
Here are \(3\) approaches to solve this problem in Java, which is Brute Force, HashMap and Sliding Window.
Brute Force
Easy, using \(2\) loops to determine whether there is the same number.
obviously, it will time out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicbooleancontainsNearbyDuplicate(int[] nums, int k) { if (nums == null || nums.length <= 1 || k <= 0) { returnfalse; }
intlen= nums.length; for (inti=0; i < len; i++) { for (intj= i + 1; j < len; j++) { if (nums[i] == nums[j]) { if (Math.abs(i - j) <= k) { returntrue; } } } }
returnfalse; }
Analysis
Time Complexity: \(O(n^2)\)
Space Complexity: \(O(1)\)
HashMap
We use a \(\texttt{HashMap}\) to record the maximum index of each element. When scanning the array from left to right, the process:
If an element \(\textit{nums}[i]\) already exists in the hash table and the index \(j\) of the element recorded in the hash table satisfies \(abs(i - j) \le k\), return \(\text{true}\);
Recording \(\textit{nums}[i]\) and index \(i\) into the hash table, where \(i\) is the largest index of \(\textit{nums}[i]\).
The order of the above two-step operations cannot be changed, when the index \(i\) is traversed, the element equal to the current element and the maximum index of the element can only be found in the elements before the index \(i\).
So we can use the exponent \(\exp\) and logarithm \(\ln\) to calculate the square root of the number \(\sqrt{x}\).
It’s really a fast and simple way!
Note: Since the computer can’t store the exact value of the float number, and the parameters and return values of the exponential function and logarithmic function are float numbers, so the result may be wrong.
For example, when \(x = 2147395600\), the result of \(e^{\frac{1}{2} \ln x}\) is \(10^{-11}\) from the correct value of \(46340\) , so when taking the integer part of the result, you will get the wrong result of \(46339\) .
So after getting the integer part \(\textit{ans}\) of the result, we should find out which of \(\textit{ans}\) and \(\textit{ans} + 1\) is the real answer.
We know how to find \(2.0\) raised to the power \(10\). The easiest way is to multiply \(10\) times \(2.0\) by loop, but what if we have to find \(2.0\) raised to the power very large number such as \(10000\) or more?
We will discuss how to find the solution of such problems by using an fast, efficient algorithm.
Brute Force
We multiply \(a\) to itself \(b\) times. That is, \(a^b = \underbrace{a \times a \dots \times a}_b\).