1 How to solve Fizz Buzz | Javascript | Java
The Given an integer n
, return a string array answer
(1-indexed) where:
answer[i] == "FizzBuzz"
ifi
is divisible by3
and5
.answer[i] == "Fizz"
ifi
is divisible by3
.answer[i] == "Buzz"
ifi
is divisible by5
.answer[i] == i
(as a string) if none of the above conditions are true.
Example 1:
Input: n = 3 Output: ["1","2","Fizz"]
Example 2:
Input: n = 5 Output: ["1","2","Fizz","4","Buzz"]
Example 3:
Input: n = 15 Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints:
1 <= n <= 104
Solution:
Javascript:
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
let output = [];
for(let i=1;i<=n;i++) {
if(i%3 == 0 && i%5==0) {
output.push("FizzBuzz");
} else if(i%3 == 0) {
output.push("Fizz");
} else if (i%5 == 0) {
output.push("Buzz");
} else {
output.push(''+i);
}
}
return output;
};
Java
class Solution {
public List<String> fizzBuzz(int n) {
List<String> ans = new ArrayList<String>();
for (int num = 1; num <= n; num++) {
boolean divBy3 = (num % 3 == 0);
boolean divBy5 = (num % 5 == 0);
if (divBy3 && divBy5) {
// Divides by both 3 and 5, add FizzBuzz
ans.add("FizzBuzz");
} else if (divBy3) {
// Divides by 3, add Fizz
ans.add("Fizz");
} else if (divBy5) {
// Divides by 5, add Buzz
ans.add("Buzz");
} else {
// Not divisible by 3 or 5, add the number
ans.add(Integer.toString(num));
}
}
return ans;
}
}
2 Palindrome Number | javascript | java
Given an integer x
, return true
if x
is a
palindrome, and false
otherwise.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -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: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Follow up: Could you solve it without converting the integer to a string?
Solution javascript:
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if(x < 0) {
return false;
}
let number = x;
let reverse = 0;
while(x > 0) {
reverse = reverse *10 + number%10;
number = parseInt(number/10);
}
return reverse == x;
};
Solution Java:
public class PalindromeNum {
public boolean isPalindrome(int x) {
// range condition
if (x < 0) {
return false;
}
// Store the number in a variable
int number = x;
// This will store the reverse of x
int reverse = 0;
while (number > 0) {
reverse = reverse * 10 + number % 10;
number /= 10;
}
return x == reverse;
}
}
3 Factorial Trailing Zeroes
Given an integer n
, return the number of trailing zeroes in n!
.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
.
Example 1:
Input: n = 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: n = 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Example 3:
Input: n = 0 Output: 0
Constraints:
0 <= n <= 104
Follow up: Could you write a solution that works in logarithmic time complexity?
Solution Javascript:
/**
* @param {number} n
* @return {number}
*/
var trailingZeroes = function(n) {
count = 0;
while(n>0) {
n = parseInt(n/5);
count = count + n;
}
return count;
};
Solution java:
class Solution {
public int trailingZeroes(int n) {
int count = 0;
while (n > 0) {
n /= 5;
count += n;
}
return count;
}
}
4 Pow(x, n)
Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
Example 1:
Input: x = 2.00000, n = 10 Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3 Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25
Constraints:
-100.0 < x < 100.0
-231 <= n <= 231-1
n
is an integer.- Either
x
is not zero orn > 0
. -104 <= xn <= 104
Solution Java:
class Solution {
public double myPow(double x, int n) {
double ans = 1.0;
long num = n;
if (n < 0) {
num = -1 * num;
}
while (num > 0) {
if (num % 2 == 0) {
x = x * x;
num = num / 2;
} else {
ans = ans * x;
num = num - 1;
}
}
if (n < 0) {
return (double)(1.0) / (double)(ans);
}
return ans;
}
}