Problème
Return n multiplied by itself. The square of a negative number is positive.
Implement square_number(n).
Input
n: Any integer in the allowed range.
Output
Return n × n.
Contraintes
−3,000,000,000 ≤ n ≤ 3,000,000,000, so the square fits a signed 64-bit integer.
Exemples
Exemple 1
Entrée :
n = 12
Sortie attendue :
expected = 144
Entrée :
long long n = 12;
Sortie attendue :
long long expected = 144;
Entrée :
long n = 12;
Sortie attendue :
long expected = 144;
Explication
12 × 12 = 144.
Exemple 2
Entrée :
n = -7
Sortie attendue :
expected = 49
Entrée :
long long n = -7;
Sortie attendue :
long long expected = 49;
Entrée :
long n = -7;
Sortie attendue :
long expected = 49;
Explication
(−7) × (−7) = 49.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
This is a direct multiplication problem, even for negative inputs.
Indice 2
A number times itself always gives a nonnegative result.
Indice 3
Watch for overflow in languages with fixed-size integers, though the result fits 64 bits here.
Complexité attendue
O(1) time, O(1) extra space