Skip to content

Commit 67744e9

Browse files
committed
practice to slove a problem
1 parent 2b1cb32 commit 67744e9

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

javascript/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,21 +1061,27 @@ let numerator = 1, denominator = 2;
10611061
* @return {number[]}
10621062
*/
10631063
var findXSum = function(nums, k, x) {
1064-
// 將nums切成長度 = k的,並計算在裡面有幾個是連續子陣列且元素出現次數 = x的
1064+
// 將nums切成長度 = k的陣列,並計算在該陣列裡面有幾個元素出現次數 = x的
10651065
// 最後將符合的加總,並組成新陣列,形成元素i
1066-
let res = [];
1067-
let map = new Map();
1068-
1069-
for(let i = 0;i < k;++i) {
1070-
map.has(nums[i]) ? map.set(nums[i],map.get(nums[i]) + 1) : map.set(nums[i],1);
1071-
}
1066+
let res = [];
1067+
// let map = new Map();
1068+
// for(let i = 0;i < k;++i) {
1069+
// map.has(nums[i]) ? map.set(nums[i],map.get(nums[i]) + 1) : map.set(nums[i],1);
1070+
// }
1071+
let i = 0;
1072+
while(i < k){
1073+
let sub = nums[i];
1074+
console.log(sub);
1075+
i++;
1076+
}
1077+
// for(let i = 0;i < k;++i) {
1078+
// console.log(nums[i])
1079+
// }
10721080

10731081
};
1074-
// let nums = [1,1,2,2,3,4,2,3], k = 6, x = 2;
1082+
let nums = [1,1,2,2,3,4,2,3], k = 6, x = 2;
10751083
// [6,10,12]
10761084
// For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.
10771085
// For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.
10781086
// For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.
1079-
// console.log(findXSum(nums,k,x));
1080-
1081-
1087+
console.log(findXSum(nums,k,x));

0 commit comments

Comments
 (0)