Skip to content

Commit 54091e6

Browse files
author
zongyanqi
committed
add Easy_202_Happy_Number Easy_263_Ugly_Number Easy_461_Hamming_Distance Easy_476_Number_Complement Easy_520_Detect_Capital.js
1 parent 63ae02f commit 54091e6

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

Easy_202_Happy_Number.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Write an algorithm to determine if a number is "happy".
3+
* A happy number is a number defined by the following process: Starting with any positive integer,
4+
* replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay),
5+
* or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
6+
*
7+
* Example: 19 is a happy number
8+
*
9+
* 1^2 + 9^2 = 82
10+
* 8^2 + 2^2 = 68
11+
* 6^2 + 8^2 = 100
12+
* 1^2 + 0^2 + 0^2 = 1
13+
*
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @return {boolean}
19+
*/
20+
var isHappy = function (n) {
21+
22+
var num = n;
23+
var results = [];
24+
while (true) {
25+
var digits = ('' + num).split('');
26+
var newN = squareSumOfDigits(digits);
27+
if (newN === 1) return true;
28+
if (results.indexOf(newN) > -1) return false;
29+
results.push(newN);
30+
num = newN;
31+
}
32+
};
33+
34+
function squareSumOfDigits(digits) {
35+
return digits.reduce((sum, d) => sum += d * d, 0);
36+
}
37+
38+
console.log(isHappy(1));
39+
console.log(isHappy(19));

Easy_263_Ugly_Number.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/ugly-number/#/description
4+
*
5+
* Write a program to check whether a given number is an ugly number.
6+
*
7+
* Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
8+
*
9+
* For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
10+
*
11+
* Note that 1 is typically treated as an ugly number.
12+
*/
13+
14+
/**
15+
* @param {number} num
16+
* @return {boolean}
17+
*/
18+
var isUgly = function (num) {
19+
if (num <= 0) return false;
20+
if (num == 1) return true;
21+
22+
while (num > 1) {
23+
var old = num;
24+
if (!(num % 2)) num = num / 2;
25+
if (!(num % 3)) num = num / 3;
26+
if (!(num % 5)) num = num / 5;
27+
if (old === num) return false;
28+
}
29+
return true;
30+
31+
};
32+
33+
console.log(isUgly(6) === true);
34+
console.log(isUgly(8) === true);
35+
console.log(isUgly(14) === false);

Easy_461_Hamming_Distance.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
*
3+
* The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
4+
5+
Given two integers x and y, calculate the Hamming distance.
6+
7+
Note:
8+
0 ≤ x, y < 231.
9+
10+
Example:
11+
12+
Input: x = 1, y = 4
13+
14+
Output: 2
15+
16+
Explanation:
17+
1 (0 0 0 1)
18+
4 (0 1 0 0)
19+
↑ ↑
20+
21+
The above arrows point to positions where the corresponding bits are different.
22+
23+
*/
24+
25+
/**
26+
* @param {number} x
27+
* @param {number} y
28+
* @return {number}
29+
*/
30+
var hammingDistance = function (x, y) {
31+
32+
var z = x ^ y;
33+
var ret = 0;
34+
while (z) {
35+
ret += z % 2;
36+
z = Math.floor(z / 2);
37+
}
38+
return ret;
39+
40+
};
41+
42+
console.log(hammingDistance(1, 4));

Easy_476_Number_Complement.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
*
3+
*
4+
* Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
5+
* Note:
6+
*
7+
* The given integer is guaranteed to fit within the range of a 32-bit signed integer.
8+
* You could assume no leading zero bit in the integer’s binary representation.
9+
*
10+
* Example:
11+
* Input: 5
12+
* Output: 2
13+
* Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
14+
*/
15+
16+
/**
17+
* @param {number} num
18+
* @return {number}
19+
*/
20+
var findComplement = function (num) {
21+
var str = '';
22+
23+
while (num) {
24+
str = ((num % 2) ? 0 : 1) + str;
25+
num = Math.floor(num / 2);
26+
}
27+
28+
return parseInt(str, 2);
29+
};
30+
31+
console.log(findComplement(5));
32+
console.log(findComplement(2));

Easy_520_Detect_Capital.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Given a word, you need to judge whether the usage of capitals in it is right or not.
3+
4+
We define the usage of capitals in a word to be right when one of the following cases holds:
5+
6+
All letters in this word are capitals, like "USA".
7+
All letters in this word are not capitals, like "leetcode".
8+
Only the first letter in this word is capital if it has more than one letter, like "Google".
9+
Otherwise, we define that this word doesn't use capitals in a right way.
10+
11+
*/
12+
13+
/**
14+
* @param {string} word
15+
* @return {boolean}
16+
*/
17+
var detectCapitalUse = function (word) {
18+
19+
var len = word.length;
20+
var upperLen = 0;
21+
var lowerLen = 0;
22+
var firstCap = false;
23+
for (var i = 0; i < len; i++) {
24+
var ch = word[i];
25+
if (/[A-Z]/.test(ch)) {
26+
upperLen++;
27+
if (i == 0) {
28+
firstCap = true;
29+
}
30+
}
31+
if (/[a-z]/.test(ch)) {
32+
lowerLen++;
33+
}
34+
}
35+
if (upperLen == len) return true;
36+
if (lowerLen == len) return true;
37+
return (upperLen == 1 && firstCap && len > 1);
38+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy