Skip to content

Instantly share code, notes, and snippets.

@buymed-hoangpham
Last active September 9, 2022 13:43
Show Gist options
  • Save buymed-hoangpham/7bb92365d3f5c71752555efee9b83810 to your computer and use it in GitHub Desktop.
Save buymed-hoangpham/7bb92365d3f5c71752555efee9b83810 to your computer and use it in GitHub Desktop.
Leetcode Practice
// 412. Fizz Buzz
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
let arr = [];
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
arr.push('Fizz Buzz');
continue;
}
if (i % 3 === 0) {
arr.push('Fizz');
continue;
}
if (i % 5 === 0) {
arr.push('Buzz');
continue;
}
arr.push(i.toString());
}
return arr
};
// 1704. Determine if String Halves Are Alike
/**
* @param {string} s
* @return {boolean}
*/
var halvesAreAlike = function(s) {
const vowel = {a: "a", e: "e", i: "i", o: "o", u: "u"};
s = s.toLowerCase();
let count = 0;
for(let i = 0; i < s.length / 2; i++) {
if(vowel[s[i]]) {
count ++
}
}
for(let i = s.length / 2; i < s.length; i++) {
if(vowel[s[i]]) {
count --
}
}
return count === 0;
};
// 1662. Check If Two String Arrays are Equivalent
/**
* @param {string[]} word1
* @param {string[]} word2
* @return {boolean}
*/
var arrayStringsAreEqual = function(word1, word2) {
const word1String = word1.join("");
const word2String = word2.join("");
return word1String === word2String
};
// 1512. Number of Good Pairs
/**
* @param {number[]} nums
* @return {number}
*/
var numIdenticalPairs = function(nums) {
let count = 0;
for(let i = 0; i < nums.length; i++){
for(let j = i + 1; j < nums.length; j++) {
if(nums[i] == nums[j]){
count ++
}
}
}
return count;
};
// 1480. Running Sum of 1d Array
/**
* @param {number[]} nums
* @return {number[]}
*/
var runningSum = function(nums) {
let rs = [nums[0]]
for (let i = 1; i < nums.length; i++)
rs[i] = rs[i-1] + nums[i]
return rs;
};
// 1470. Shuffle the Array
/**
* @param {number[]} nums
* @param {number} n
* @return {number[]}
*/
var shuffle = function(nums, n) {
let res = []
for(let i = 0; i < n; i++){
res.push(nums[i], nums[i+n])
}
return res
};
// 1464. Maximum Product of Two Elements in an Array
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function(nums) {
let i = 0;
let j = 0;
let maxFirst = nums[0];
let maxSecond = nums[0];
if(nums[0] > nums[1]) {
maxFirst = nums[0];
i = 0;
maxSecond = nums[1];
j= 1;
} else {
maxFirst = nums[1];
i = 1;
maxSecond = nums[0];
j = 0;
}
for(let k = 2; k < nums.length; k++) {
if(nums[k] > maxFirst) {
maxSecond = maxFirst;
maxFirst = nums[k];
j = i;
i = k;
} else if(nums[k] > maxSecond) {
maxSecond = nums[k];
j = k;
}
}
return (nums[i] - 1)*(nums[j] - 1);
};
// 1431. Kids With the Greatest Number of Candies
/**
* @param {number[]} candies
* @param {number} extraCandies
* @return {boolean[]}
*/
var kidsWithCandies = function(candies, extraCandies) {
let max = 1;
for(let i = 0; i < candies.length; i++) {
if(candies[i] > max) max = candies[i];
}
let result = [];
for(let i = 0; i < candies.length; i++) {
if(candies[i] + extraCandies >= max) {
result.push(true)
continue;
}
result.push(false)
}
return result;
};
// 1365. How Many Numbers Are Smaller Than the Current Number
/**
* @param {number[]} nums
* @return {number[]}
*/
var smallerNumbersThanCurrent = function(nums) {
let result = [];
for(let i = 0; i < nums.length; i++) {
let count = 0;
for(let j = 0; j < nums.length; j++){
if(nums[i] > nums[j] && i !== j) {
count++;
}
}
result.push(count)
}
return result;
};
// 1295. Find Numbers with Even Number of Digits
/**
* @param {number[]} nums
* @return {number}
*/
var findNumbers = function(nums) {
let count = 0;
for(let i = 0; i < nums.length; i++){
if(nums[i].toString().length % 2 === 0) count++;
}
return count;
};
// 1822. Sign of the Product of an Array
/**
* @param {number[]} nums
* @return {number}
*/
var arraySign = function(nums) {
let check = nums[0] > 0 ? true : false;
if(nums[0] == 0) {
return 0;
}
for(let i = 1; i < nums.length; i++){
if(nums[i] == 0) return 0;
if(nums[i] < 0) {
check = !check;
}
}
return check? 1: -1
};
@QuocCao-dev
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment