https://leetcode.com/problems/convert-to-base-2/
https://leetcode.com/problems/convert-to-base-2/discuss/265507/JavaC%2B%2BPython-2-lines-Exactly-Same-as-Base-2
Note that I didn't deal with string concatenation,
and just take this part as
https://leetcode.com/problems/convert-to-base-2/discuss/266760/Java-simple-3-lines
https://leetcode.com/problems/convert-to-base-2/discuss/265688/4-line-python-clear-solution-with-explanation
https://leetcode.com/problems/convert-to-base-2/discuss/265544/C%2B%2B-Geeks4Geeks
https://www.geeksforgeeks.org/convert-base-decimal-vice-versa/
Given a number
N
, return a string consisting of "0"
s and "1"
s that represents its value in base -2
(negative two).
The returned string must have no leading zeroes, unless the string is
"0"
.
Example 1:
Input: 2 Output: "110" Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
Example 2:
Input: 3 Output: "111" Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3
Example 3:
Input: 4 Output: "100" Explantion: (-2) ^ 2 = 4
Note:
0 <= N <= 10^9
Intuition:
- Maybe write a base2 function first?
- How about add minus
-
? - Done.
Explanation:
base2 function is quite basis of basis.
check last digit, shift to right.
base-2 is totally no difference, just add a sign
check last digit, shift to right.
base-2 is totally no difference, just add a sign
-
.Time Complexity:
O(logN)
Time, O(logN)
space.Note that I didn't deal with string concatenation,
and just take this part as
O(1)
.
Instead of create a new string each time,
we can improve this process using some operations join/reverse or data structure list/vector .
Like Java we may need
but those are not what I want to discuss deeply here.
we can improve this process using some operations join/reverse or data structure list/vector .
Like Java we may need
StringBuilder
,but those are not what I want to discuss deeply here.
public String base2(int N) {
String res = "";
while (N != 0) {
res = Integer.toString(N & 1) + res;
N = N >> 1;
}
return res == "" ? "0" : res;
}
public String baseNeg2(int N) {
String res = "";
while (N != 0) {
res = Integer.toString(N & 1) + res;
N = -(N >> 1);
}
return res == "" ? "0" : res;
}
base2 won't work for negative number, instead of >> should be >>>https://leetcode.com/problems/convert-to-base-2/discuss/266760/Java-simple-3-lines
public String baseNeg2(int N) {
StringBuilder ans = new StringBuilder( N==0 ? "0" : "" );
for( ; N!=0; N=-(N>>1) ) ans.append( (N&1)==0 ? '0' : '1' );
return ans.reverse().toString();
}
https://leetcode.com/problems/convert-to-base-2/discuss/265688/4-line-python-clear-solution-with-explanation
for each number, we first consider its binary mode, then we check 10,1000,100000,... one by one
for example, 6 = '110', so for the second '1', in base -2, it makes the initial number decrease
so we jusr make the initial number add 4, then 6 -> 10 = '1010', later we consider the first '1', it makes the initial number decrease
for example, 6 = '110', so for the second '1', in base -2, it makes the initial number decrease
4(+2 -> -2)
so we jusr make the initial number add 4, then 6 -> 10 = '1010', later we consider the first '1', it makes the initial number decrease
16(+8 -> -8)
, then we add 16, 10 -> 26 = '11010', now we get the answer.class Solution:
def baseNeg2(self, N: int) -> str:
neg = [1 << i for i in range(1, 33, 2)]
for mask in neg:
if N & mask: N += mask*2
return bin(N)[2:]
https://leetcode.com/problems/convert-to-base-2/discuss/265544/C%2B%2B-Geeks4Geeks
See this Geeks4Geeks article: Convert a number into negative base representation.
Below is the specialized solution for the
-2
base.string baseNeg2(int N, string res = "") {
while (N != 0) {
int rem = N % -2;
N /= -2;
if (rem < 0) rem += 2, N += 1;
res = to_string(rem) + res;
}
return max(string("0"), res);
}
https://www.geeksforgeeks.org/convert-number-negative-base-representation/
A number n and a negative base negBase is given to us, we need to represent n in that negative base. Negative base works similar to positive base. For example in base 2 we multiply bits to 1, 2, 4, 8 and so on to get actual number in decimal. In case of base -2 we need to multiply bits with 1, -2, 4, -8 and so on to get number in decimal.
Examples:
Input : n = 13, negBase = -2 Output : 11101 1*(16) + 1*(-8) + 1*(4) + 0*(-2) + 1*(1) = 13
It is possible to represent a number into any negative base with same procedure (Refer Wiki for details). For simplicity (to get rid of A, B etc characters in output), we are allowing our base to be in between -2 and -10 only.
We can solve this problem similar to solving problem with positive bases but one important thing to remember is, remainder will always be positive whether we work with positive base or negative base but in most compilers, the result of dividing a negative number by a negative number is rounded towards 0, usually leaving a negative remainder.
So whenever we get a negative remainder, we can convert it to positive as below,
So whenever we get a negative remainder, we can convert it to positive as below,
Let n = (?negBase) * quotient + remainder = (?negBase) * quotient + negBase ? negBase + negBase = (?negBase) * (quotient + 1) + (remainder + negBase). So if after doing "remainder = n % negBase" and "n = n/negBase", we get negative remainder, we do following. remainder = remainder + (-negBase) n = n + 1 Example : n = -4, negBase = -3 In C++, we get remainder = n % negBase = -4/-3 = -1 n = n/negBase [Next step for base conversion] = -4/-3 = 1 To avoid negative remainder, we do, remainder = -1 + (-negBase) = -1 - (-3) = 2 n = n + 1 = 1 + 1 = 2.
So when we will get negative remainder, we will make it positive by adding absolute value of base to it and adding 1 to our quotient.
Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. Value of A is 10, value of B is 11 and so on. Write a function to convert the number to decimal.