Skip to content

Commit 48503bb

Browse files
committed
practice,not done
1 parent c8550fa commit 48503bb

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

javascript/index.js

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ var mySqrt = function(x) {
11111111
// rounded down to the nearest integer.
11121112

11131113
};
1114-
let x = 8;
1114+
// let x = 8;
11151115
// 2
11161116
// console.log(mySqrt(x));
11171117

@@ -1143,11 +1143,20 @@ var specialTriplets = function(nums) {
11431143

11441144
// }
11451145

1146-
let left = new Map();
1147-
let right = new Map();
1146+
/**
1147+
* j = middle index
1148+
* 在j之前,檢查nums[i] === nums[j] * 2 的有幾個
1149+
* 在j之後,檢查nums[k] === nums[j] * 2 的有幾個
1150+
*/
1151+
// let left = new Map();
1152+
// let right = new Map();
1153+
let j = Math.round(nums.length % 2);
11481154
for(let i = 0;i < nums.length;++i) {
1149-
1155+
if(nums[i] === nums[j] * 2 && i < j){
1156+
ans++;
1157+
}
11501158
}
1159+
console.log(ans)
11511160
};
11521161
let nums = [8,4,2,8,4];
11531162
/**
@@ -1164,4 +1173,43 @@ let nums = [8,4,2,8,4];
11641173
* nums[1] = nums[2] * 2 = 2 * 2 = 4
11651174
* nums[4] = nums[2] * 2 = 2 * 2 = 4
11661175
*/
1167-
console.log(specialTriplets(nums));
1176+
// console.log(specialTriplets(nums));
1177+
1178+
1179+
/**
1180+
* 1779. Find Nearest Point That Has the Same X or Y Coordinate
1181+
*
1182+
* x & y = current location
1183+
* A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
1184+
* 沒有回傳-1
1185+
*
1186+
* The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
1187+
*
1188+
* @param {number} x
1189+
* @param {number} y
1190+
* @param {number[][]} points
1191+
* @return {number}
1192+
*/
1193+
var nearestValidPoint = function(x, y, points) {
1194+
1195+
// abs(x-validPoint[0]) or abs(y-validPoint[1]) .
1196+
let totalDistance = x + y;
1197+
let ans = [];
1198+
for(const p of points) {
1199+
let prev = parseInt(p[0]),next = parseInt(p[1]);
1200+
if(prev === x || next === y){
1201+
let distance = Math.abs(x - prev) + Math.abs(y - next);
1202+
ans.push(distance);
1203+
}
1204+
1205+
}
1206+
console.log(ans)
1207+
};
1208+
let x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]];
1209+
// 2
1210+
// Of all the points, only [3,1], [2,4] and [4,4] are valid.
1211+
// Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1.
1212+
// [2,4] has the smallest index, so return 2.
1213+
// let x = 3, y = 4, points = [[3,4]];
1214+
// 0
1215+
console.log(nearestValidPoint(x,y,points));

0 commit comments

Comments
 (0)