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

Commitf304527

Browse files
committed
add 299
1 parent4d1c6a2 commitf304527

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

‎Easy/205-isomorphicStrings.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 1. check if t[i] is in the map. If yes, find out the key associated with t[i]
88
* if key exists, but s[i] is not equal to key, return false;
99
* if key is null, but s[i] exists in the map and map[s[i]] is not equal to t[i]
10-
* return false.
10+
* return false.
1111
*
1212
*@param {string} s
1313
*@param {string} t
@@ -33,7 +33,7 @@ var isIsomorphic = function(s, t) {
3333
Object.prototype.getKeyByValue=function(value){
3434
for(varpropinthis){
3535
if(this.hasOwnProperty(prop)){
36-
if(this[prop]===value)
36+
if(this[prop]===value)
3737
returnprop;
3838
}
3939
}

‎Easy/242-anagram.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// remember: anagram also requires the appearance of the letter in both strings is same.
77
varisAnagram=function(s,t){
88
varisAnagram=true;
9-
if(s.length!==t.length){returnfalse;}
9+
if(s.length!==t.length)returnfalse;
1010
varsObject={};
1111
for(vari=0;i<s.length;i++){
1212
sObject[s[i]] ?sObject[s[i]]++ :(sObject[s[i]]=1);
@@ -19,7 +19,7 @@ var isAnagram = function(s, t) {
1919
if(!tObject.hasOwnProperty(key)){
2020
isAnagram=false;
2121
break;
22-
}elseif(sObject[key]!==tObject[key]){
22+
}elseif(sObject[key]!==tObject[key]){
2323
isAnagram=false;
2424
break;
2525
}

‎Easy/299-bullsandCows.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Key two rounds
3+
* 1. first round scan to build the map (number -> appearances), if secret[i] === guess[i], bull++
4+
* if it's not a bull, increase the cow appearances in the map
5+
* 2. second round, skip the bull positions and compare the guess number and the map
6+
* if there is a cow, cow++ and decrease the cow number from the map. if the cow number is 0
7+
* that is, there is matched cow number between secrect and guess.
8+
*
9+
*@param {string} secret
10+
*@param {string} guess
11+
*@return {string}
12+
*/
13+
vargetHint=function(secret,guess){
14+
varsecretMap={};
15+
varcow=0;
16+
varbull=0;
17+
18+
for(vari=0;i<secret.length;i++){
19+
if(secret[i]===guess[i])bull++;
20+
elseif(secretMap[secret[i]])secretMap[secret[i]]++;
21+
elsesecretMap[secret[i]]=1;
22+
}
23+
for(varj=0;j<guess.length;j++){
24+
console.log(secretMap[guess[j]]);
25+
if(guess[j]!==secret[j]&&secretMap[guess[j]]){
26+
cow++;
27+
secretMap[guess[j]]--;
28+
}
29+
}
30+
31+
returnbull+'A'+cow+'B';
32+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp