0%

題目

1
2
3
4
5
6
7
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1
Explanation: We have 3 students where:
The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.
The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.
The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

解法思維

做出endTimestartTime的陣列在查找

1
2
3
4
5
6
7
8
9
10
11
12
var busyStudent = function(startTime, endTime, queryTime) {
let sum=0
for (let j = 0; j < startTime.length; j++) {
let len=endTime[j]-startTime[j]
if(len==0){
Array.from({length:1},(v,i)=>endTime[j]).indexOf(queryTime)!=-1?sum++:false;
}else{
Array.from({length:len+1}, (v, i) => startTime[j]++).indexOf(queryTime)!=-1?sum++:false;
}
}
return sum
};

如果startTime小於目標時間和endTime大於目標時間,就代表目標時間包含在裡面

1
2
3
4
5
6
7
const busyStudent = (startTime, endTime, queryTime) => {
let ret = 0;
for (let i = 0; i < startTime.length; ++i) {
startTime[i] <= queryTime && endTime[i] >= queryTime && ++ret;
}
return ret;
};

題目

1
2
3
4
5
6
7
8
9
10
Input: nums = [12,345,2,6,7896]
Output: 2
Explanation:
12 contains 2 digits (even number of digits).
345 contains 3 digits (odd number of digits).
2 contains 1 digit (odd number of digits).
6 contains 1 digit (odd number of digits).
7896 contains 4 digits (even number of digits).
Therefore only 12 and 7896 contain an even number of digits.

解法思維

先變成字串在看長度判斷

1
2
3
4
5
6
7
var findNumbers = function(nums) {
let res=0
nums.forEach(item => {
item.toString().length % 2==0?res++:false
});
return res
};

題目

1
2
3
4
5
6
7
Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

解法思維

兩點相減再取最大數

1
2
3
4
5
6
7
8
9
var minTimeToVisitAllPoints = function(points) {
let res=0
for (let i = 0; i < points.length-1; i++) {
let x=Math.abs(points[i][0]-points[i+1][0])
let y=Math.abs(points[i][1]-points[i+1][1])
res+= Math.max(x,y)
}
return res
};

題目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Input
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
Output
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]

Explanation
// Note that the values ordered by ID is ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"].
OrderedStream os = new OrderedStream(5);
os.insert(3, "ccccc"); // Inserts (3, "ccccc"), returns [].
os.insert(1, "aaaaa"); // Inserts (1, "aaaaa"), returns ["aaaaa"].
os.insert(2, "bbbbb"); // Inserts (2, "bbbbb"), returns ["bbbbb", "ccccc"].
os.insert(5, "eeeee"); // Inserts (5, "eeeee"), returns [].
os.insert(4, "ddddd"); // Inserts (4, "ddddd"), returns ["ddddd", "eeeee"].
// Concatentating all the chunks returned:
// [] + ["aaaaa"] + ["bbbbb", "ccccc"] + [] + ["ddddd", "eeeee"] = ["aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"]
// The resulting order is the same as the order above.

解法思維

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var OrderedStream = function(n) {
this.parts = new Array(n);
this.head = 0;
};

OrderedStream.prototype.insert = function(id, value) {
this.parts[id-1] = value;

let res = [];
if (id-1 === this.head) {
while (this.parts[this.head]) {
res.push(this.parts[this.head++]);
}
}
return res;
};

題目

1
2
3
4
5
6
7
8
9
10
11
12
13
Input: arr = [1,4,2,5,3]
Output: 58
Explanation: The odd-length subarrays of arr and their sums are:
[1] = 1
[4] = 4
[2] = 2
[5] = 5
[3] = 3
[1,4,2] = 7
[4,2,5] = 11
[2,5,3] = 10
[1,4,2,5,3] = 15
If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58

解法思維

利用slice()取出奇數列再加總

1
2
3
4
// 取頭不取尾
let arr=[1,2,3,4]
arr.slice(0,1) // 1
arr.slice(0,3) // 1,2,3
1
2
3
4
5
6
7
8
9
10
11
const sumOddLengthSubarrays= arr => {
let sum = 0;
for (let i = 1; i <= arr.length; i += 2) {
for (let j = 0; j <= arr.length - i; j++) {
let odd = arr.slice(j, j + i);
console.log(j,odd)
sum += odd.reduce((acc, cv) => acc + cv, 0);
}
}
return sum;
};

題目

1
2
3
4
5
6
7
8
9
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output: [0,4,1,3,2]
Explanation:
nums index target
0 0 [0]
1 1 [0,1]
2 2 [0,1,2]
3 2 [0,1,3,2]
4 1 [0,4,1,3,2]

解法思維

運用splice(索引位置, 要刪除元素的數量, 元素)

1
2
3
4
5
6
7
var createTargetArray =(nums, index)=> {
let result=[]
for (let i = 0; i < nums.length; i++) {
result.splice(index[i],0,nums[i])
}
return result
};

要求

  • 020 秒為 1位數計算 ,2140 秒為 2 位數計算,41~60 秒為 3 位數計算,加減乘除規則請用隨機產生,不可寫死題目,60 秒內可無限次數答題。
  • 040 秒答對加一分,4160 秒答對加五分,答錯扣一分,最多僅能扣到零分
  • 不可設計跳轉頁面,都得在同一頁內部切換頁面完成。

    攻略

  • 利用setInterval()達到每秒更新
  • 利用Math.floor() Math.random() 達成亂數規則

製作一個倒數計時器

這邊踩到的雷是換轉頁面時,setInterval()還在執行,
clearInterval(timeId),必須要宣告個變數接timeId
不可直接帶入,這裡卡了好久 😭😭 ,附上相關連結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
let sec
let timeId

function getTime() {
if(main.classList[1]=='d-none')return
if(sec==0){
main.classList.add('d-none')
restart.classList.remove('d-none')
getStatus(main.classList[1])
return
}
else if(sec<=10){
sec -=1
time.textContent=`00 : 0${sec}`
}else{
sec -=1
time.textContent=`00 : ${sec}`
}
}

function getStatus(status) {
if(status!='d-none'){
timeId=setInterval(getTime,1000)

}else if(status=='d-none'){
clearInterval(timeId)
}
}

製作一個亂數選擇器

Math.random()會產生 0~1隨機小數,再利用Math.floor()取最大整數

1
2
3
4
5
6
Math.random(); // 0.23012228691419123 
Math.random(); // 0.8107096418156079

Math.floor(Math.random()); //回傳0
Math.floor(0.8888888); //回傳0

當我們把Math.random()*2放進Math.floor()中會得到 0、1 的結果。

1
2
3
4
(Math.random()*2 ) // 0.000...02~1.999...98
Math.floor(Math.random()*2); //回傳0或1
Math.floor(Math.random()*5); //回傳0或1或2或3或4
Math.floor(Math.random()*50); //回傳0或1或2或3...或49

我們來實作 min~max 之間的亂數,min 為最小值 max為最大值。

1
2
3
4
5
6
7
8
9
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
};

//會產生1~10之間的隨機亂數
getRandom(1,10);
//會產生100~500之間的隨機亂數
getRandom(100,500);

題目

1
2
3
4
5
Input: nums = [1,2,3,4]
Output: [2,4,4,4]
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].
At the end the concatenation [2] + [4,4,4] is [2,4,4,4].

解法思維

1
2
3
4
5
6
7
8
9
10
11
12
var decompressRLElist = function(nums) {
let arr = [];
function pushTo(count, num) {
if (count === 0) return;
arr.push(num);
pushTo(count-1, num);
}
for (let i = 0; i < nums.length; i += 2) {
pushTo(nums[i], nums[i+1]);
}
return arr;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
var decompressRLElist = function(nums) {
let result = []

for(let i=0; i<nums.length; i+=2) {
const frequency = nums[i]
const value = nums[i+1]

// result = [...result, ...new Array(frequency).fill(value)]
result.push(...new Array(frequency).fill(value))
}

return result
};

題目

1
2
3
4
5
6
7
8
Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

解法思維

跑迴圈依序比大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const smallerNumbersThanCurrent = nums => {
let result = [];
let count = 0;

for (let i = 0; i < nums.length; i += 1) {
let cv = nums[i];

for (let j = 0; j < nums.length; j += 1) {
let num = nums[j];

if (cv > num) {
count++
}
}

result.push(count)
count = 0;
}

return result;
};

先用 sort()排序由小到大,再由indexOf查找位置,回傳結果

1
2
3
4
5
let smallerNumbersThanCurrent = (nums) =>{
let tp=Array.from(nums).sort((a,b)=>a-b)
let result=nums.map(x=>tp.indexOf(x))
return result
};

題目

1
2
3
4
5
6
7
Input: command = "G()(al)"
Output: "Goal"
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is "Goal".

解法思維

利用replace()正規表達式

1
var interpret =(command)=>command.replace(/\(\)/g,'o').replace(/\((\w)(\w)\)/g,"$1$2");