Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54091e6

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 parent63ae02f commit54091e6

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+
varisHappy=function(n){
21+
22+
varnum=n;
23+
varresults=[];
24+
while(true){
25+
vardigits=(''+num).split('');
26+
varnewN=squareSumOfDigits(digits);
27+
if(newN===1)returntrue;
28+
if(results.indexOf(newN)>-1)returnfalse;
29+
results.push(newN);
30+
num=newN;
31+
}
32+
};
33+
34+
functionsquareSumOfDigits(digits){
35+
returndigits.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+
varisUgly=function(num){
19+
if(num<=0)returnfalse;
20+
if(num==1)returntrue;
21+
22+
while(num>1){
23+
varold=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)returnfalse;
28+
}
29+
returntrue;
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+
varhammingDistance=function(x,y){
31+
32+
varz=x^y;
33+
varret=0;
34+
while(z){
35+
ret+=z%2;
36+
z=Math.floor(z/2);
37+
}
38+
returnret;
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+
varfindComplement=function(num){
21+
varstr='';
22+
23+
while(num){
24+
str=((num%2) ?0 :1)+str;
25+
num=Math.floor(num/2);
26+
}
27+
28+
returnparseInt(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+
vardetectCapitalUse=function(word){
18+
19+
varlen=word.length;
20+
varupperLen=0;
21+
varlowerLen=0;
22+
varfirstCap=false;
23+
for(vari=0;i<len;i++){
24+
varch=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)returntrue;
36+
if(lowerLen==len)returntrue;
37+
return(upperLen==1&&firstCap&&len>1);
38+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp