Skip to content

Commit a63a166

Browse files
committed
add no.2515
1 parent 9abb6c9 commit a63a166

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

javascript/LeetCode/Array/2515.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 2515. Shortest Distance to Target String in a Circular Array
3+
*
4+
* 陣列是一個圓,意味著陣列頭元素可以取得陣列尾元素
5+
* 從左邊或右邊開始都能通
6+
*
7+
* @param {string[]} words
8+
* @param {string} target
9+
* @param {number} startIndex
10+
* @return {number}
11+
*/
12+
var closestTarget = function(words, target, startIndex) {
13+
/**
14+
* 若陣列中沒有元素符合target,回傳-1
15+
* 往左或往右找
16+
* 回傳最短能到words[target]的距離
17+
*/
18+
for(let i = 0;i < words.length;++i) {
19+
let right = (startIndex + i) % words.length;
20+
let left = (startIndex - i + words.length) % words.length;
21+
22+
if(words[left] === target || words[right] === target){
23+
return i;
24+
}
25+
}
26+
return -1;
27+
};
28+
let word = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
29+
/*
30+
Output: 1
31+
Explanation: We start from index 1 and can reach "hello" by
32+
- moving 3 units to the right to reach index 4.
33+
- moving 2 units to the left to reach index 4.
34+
- moving 4 units to the right to reach index 0.
35+
- moving 1 unit to the left to reach index 0.
36+
The shortest distance to reach "hello" is 1.
37+
*/
38+
console.log(closestTarget(word,target,startIndex));

javascript/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ var findAndReplacePattern = function(words, pattern) {
13131313

13141314
}
13151315
};
1316-
let word = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb";
1316+
// let word = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb";
13171317
// ["mee","aqq"]
13181318
// console.log(findAndReplacePattern(word,pattern));
13191319

@@ -1423,4 +1423,5 @@ Output: 2
14231423
Explanation:
14241424
The least frequent digits in n are 7, 2, and 5; each appears only once.
14251425
*/
1426-
console.log(getLeastFrequentDigit(n));
1426+
// console.log(getLeastFrequentDigit(n));
1427+

0 commit comments

Comments
 (0)