Skip to content

Commit 6e95916

Browse files
committed
feat: 腾讯前端笔试
1 parent 3a9a73e commit 6e95916

File tree

9 files changed

+404
-0
lines changed

9 files changed

+404
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { solve } = require('./合并链表形成环状链表')
2+
3+
let input = ''
4+
5+
process.stdin.on('data', function (data) {
6+
input += String(data)
7+
})
8+
9+
process.stdin.on('end', function () {
10+
main()
11+
})
12+
13+
function main () {
14+
const s = input.split('\n')
15+
const data = eval(s[0].replaceAll('{', '[').replaceAll('}', ']'))
16+
// console.log(data)
17+
solve(data)
18+
}
19+
/*
20+
{1,5,4,7,3,2,10}
21+
22+
23+
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
function ListNode (x) {
3+
this.val = x
4+
this.next = null
5+
}
6+
7+
/**
8+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
9+
*
10+
* @param a ListNode类一维数组 指向每段碎片的开头
11+
* @return ListNode类
12+
*/
13+
function solve (input) {
14+
const a = input
15+
const a = []
16+
for (const p of input) {
17+
const t = []
18+
let cur = p
19+
while (cur) {
20+
t.push(cur.val)
21+
cur = cur.next
22+
}
23+
a.push(t)
24+
}
25+
// map from val to node
26+
// val: [i, j]
27+
const map = {}
28+
const s = new Set()
29+
for (let i = 0; i < a.length; i++) {
30+
const arr = a[i]
31+
const n = arr.length
32+
// 首部和原来的相交
33+
if (map[arr[0]] != null) {
34+
const [x, y] = map[arr[0]]
35+
// 把当前的数移过去
36+
for (let k = 0; k < arr.length; k++) {
37+
a[x][y + k] = arr[k]
38+
map[arr[k]] = [x, y + k]
39+
}
40+
}
41+
// 尾部和原来的相交
42+
else if (map[arr[n - 1]] != null) {
43+
const [x, y] = map[arr[n - 1]]
44+
for (let k = 0; k < n; k++) {
45+
map[arr[k]] = [i, k]
46+
}
47+
// 把 a[x] 移过来
48+
for (let k = 0; k < a[x].length; k++) {
49+
arr[n + k] = a[x][y + k]
50+
map[arr[n + k]] = [i, n + k]
51+
}
52+
s.delete(x)
53+
s.add(i)
54+
} else {
55+
for (let k = 0; k < n; k++) {
56+
map[arr[k]] = [i, k]
57+
}
58+
s.add(i)
59+
}
60+
}
61+
function less (arrA, arrB) {
62+
for (let i = 0; i < Math.min(arrA.length, arrB.length); i++) {
63+
if (arrA[i] < arrB[i]) {
64+
return -1
65+
} else if (arrA[i] > arrB[i]) {
66+
return 1
67+
}
68+
}
69+
return arrA.length - arrB.length
70+
}
71+
let ans = [...s].sort((a, b) => a - b).map(i => a[i]).sort((arrA, arrB) => less(arrA, arrB)).flat(1)
72+
console.log(ans)
73+
while (ans.length >= 2 && ans[ans.length - 1] === ans[0]) {
74+
ans.pop()
75+
}
76+
let minIndex = -1; let minVal = 1e10
77+
for (let i = 0; i < ans.length; i++) {
78+
if (ans[i] < minVal) {
79+
minIndex = i
80+
minVal = ans[i]
81+
}
82+
}
83+
ans = [...ans.slice(minIndex), ...ans.slice(0, minIndex)]
84+
let ans1 = [...ans].reverse()
85+
ans1 = [...ans1.slice(ans1.length - 1), ...ans1.slice(0, ans1.length - 1)]
86+
// console.log(ans1)
87+
if (less(ans1, ans) < 0) ans = ans1
88+
console.log(ans)
89+
const root = new ListNode(ans[0])
90+
let cur = root
91+
for (let i = 0; i < ans.length; i++) {
92+
cur.next = new ListNode(ans[i])
93+
cur = cur.next
94+
}
95+
return root
96+
}
97+
module.exports = {
98+
solve: solve
99+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{10,2,3},{7,4,5,1,10},{3,7,4}]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
let input = ''
2+
3+
process.stdin.on('data', function (data) {
4+
input += String(data)
5+
})
6+
7+
process.stdin.on('end', function () {
8+
main()
9+
})
10+
11+
// 正解应该是 DP: dp[i][j]表示第i天持有j手的最大余额
12+
13+
function main () {
14+
const s = input.split('\n')
15+
const [n, m] = s[0].split(' ').map(Number)
16+
const a = s[1].split(' ').map(Number)
17+
18+
const dp = Array(n + 1).fill(0).map(() => [])
19+
dp[0][0] = m
20+
if (m >= a[0]) dp[0][1] = m - a[0]
21+
for (let i = 1; i < n; i++) {
22+
for (let j = 0; ;j++) {
23+
if (dp[i - 1][j - 1] == null || dp[i - 1][j - 1] - a[i] < 0) break
24+
dp[i][j] = dp[i][j] || 0
25+
// 第 i 天是买入
26+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] - a[i])
27+
}
28+
for (let j = 0; ;j++) {
29+
if (dp[i - 1][j + 1] == null || dp[i - 1][j + 1] - a[i] < 0) break
30+
dp[i][j] = dp[i][j] || 0
31+
// 第 i 天是卖出
32+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j + 1] - a[i])
33+
}
34+
// 不买不卖
35+
for (let j = 0; ;j++) {
36+
if (dp[i - 1][j] == null) break
37+
dp[i][j] = dp[i][j] || 0
38+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j])
39+
}
40+
}
41+
// console.log(dp)
42+
let ans = 0
43+
for (let j = 0; ;j++) {
44+
if (dp[n - 1][j] == null) break
45+
ans = Math.max(ans, dp[n - 1][j] + j * a[n - 1])
46+
}
47+
console.log(ans)
48+
}
49+
50+
/*
51+
3 100
52+
10 9 8
53+
54+
100
55+
*/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3 100
2+
10 9 8
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
let input = ''
2+
3+
process.stdin.on('data', function (data) {
4+
input += String(data)
5+
})
6+
7+
process.stdin.on('end', function () {
8+
main()
9+
})
10+
11+
function main () {
12+
const s = input.split('\n')
13+
const n = s.length - 1
14+
const data = s.slice(0, n - 1).map(line => {
15+
const a = line.split(',')
16+
a[1] = Number(a[1])
17+
return a
18+
})
19+
const [x, y, z] = s[n - 1].split(',')
20+
let ds = data.map(x => x[0])
21+
const ss = data.map(x => x[1])
22+
const xi = ds.findIndex((a) => a === x)
23+
ds.splice(xi, 1)
24+
const yi = ds.findIndex((a) => a === y)
25+
ds = [...ds.slice(0, yi + 1), x, ...ds.slice(yi + 1)]
26+
for (let i = 0; i < ds.length; i++) {
27+
console.log(`${ds[i]},${ss[i]}`)
28+
}
29+
}
30+
31+
/*
32+
a,1
33+
b,3
34+
c,6
35+
d,8
36+
e,9
37+
f,19
38+
e,a,b
39+
40+
a,1
41+
e,3
42+
b,6
43+
c,8
44+
d,9
45+
f,19
46+
*/
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<style type="text/css">
6+
* {
7+
margin: 0;
8+
padding: 0;
9+
}
10+
.normal {
11+
width: 300px;
12+
height: 300px;
13+
position: relative;
14+
}
15+
.normal > img {
16+
width: 100%;
17+
height: 100%;
18+
}
19+
.lay {
20+
width: 150px;
21+
height: 150px;
22+
background: orange;
23+
opacity: 0.5;
24+
position: absolute;
25+
left: 0;
26+
top: 0;
27+
cursor: move;
28+
display: none;
29+
}
30+
.big {
31+
width: 300px;
32+
height: 300px;
33+
position: absolute;
34+
left: 350px;
35+
top: 0px;
36+
overflow: hidden;
37+
display: none;
38+
}
39+
.big > img {
40+
width: 600px;
41+
height: 600px;
42+
position: absolute;
43+
display: block;
44+
}
45+
</style>
46+
</head>
47+
<body>
48+
<section id="magnifier"></section>
49+
50+
<script type="text/javascript">
51+
var magnifier = {
52+
init(param) {
53+
const el = param.el;
54+
const src = param.src;
55+
if (!el || !src) return;
56+
this.createElement(el, src);
57+
},
58+
createElement(el, src) {
59+
const _this = this;
60+
const normalDiv = document.createElement('div');
61+
normalDiv.className = 'normal';
62+
const normalImg = document.createElement('img');
63+
normalImg.src = src.normal;
64+
const normalSpan = document.createElement('span');
65+
normalSpan.className = 'lay';
66+
normalDiv.appendChild(normalImg);
67+
normalDiv.appendChild(normalSpan);
68+
69+
const bigDiv = document.createElement('div');
70+
bigDiv.className = 'big';
71+
const bigImg = document.createElement('img');
72+
bigImg.src = src.big;
73+
bigDiv.appendChild(bigImg);
74+
75+
el.appendChild(normalDiv);
76+
el.appendChild(bigDiv);
77+
78+
normalDiv.addEventListener('mouseover', () => {
79+
// TODO: 放大镜、被放大的图片都显示,渲染为块级元素
80+
normalSpan.style.display = 'block';
81+
bigDiv.style.display = 'block';
82+
});
83+
normalDiv.addEventListener('mouseout', () => {
84+
// TODO: 放大镜、被放大的图片都隐藏,不渲染
85+
normalSpan.style.display = 'none';
86+
bigDiv.style.display = 'none';
87+
});
88+
89+
normalDiv.addEventListener('mousemove', _moveHandler);
90+
91+
function _moveHandler(event) {
92+
event = event || window.event;
93+
_this.moveHandler(event, normalDiv, normalSpan, bigImg);
94+
}
95+
},
96+
moveHandler(event, normalDiv, normalSpan, bigImg) {
97+
// tip: 该函数处理放大镜、被放大的图片的显示区域,x、y分别为鼠标在该模块中的位置坐标
98+
99+
const scale = 2;
100+
let x = event.clientX - normalSpan.offsetWidth / 2;
101+
let y = event.clientY - normalSpan.offsetHeight / 2;
102+
// TODO: 判断临界值,重新设置放大镜、被放大的图片的位置
103+
let xMin = 0;
104+
let yMin = 0;
105+
let xMax = normalDiv.offsetWidth - normalSpan.offsetWidth;
106+
let yMax = normalDiv.offsetWidth - normalSpan.offsetWidth;
107+
if (x < xMin) {
108+
x = xMin;
109+
} else if (x > xMax) {
110+
x = xMax;
111+
}
112+
if (y < yMin) {
113+
y = yMin;
114+
} else if (y > yMax) {
115+
y = yMax;
116+
}
117+
// console.log(x, y);
118+
// console.log(normalSpan);
119+
normalSpan.style.left = x + 'px';
120+
normalSpan.style.top = y + 'px';
121+
bigImg.style.left = -x * 2 + 'px';
122+
bigImg.style.top = -y * 2 + 'px';
123+
},
124+
};
125+
magnifier.init({
126+
// TODO: 请获取id=magnifier的节点
127+
128+
el: document.getElementById('magnifier'),
129+
src: {
130+
normal:
131+
'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C',
132+
big: 'https://uploadfiles.nowcoder.com/images/20211201/920662346_1638327818015/FE8B1A979ADF6E3C2C114AF3F9CA693C',
133+
},
134+
});
135+
</script>
136+
</body>
137+
</html>

0 commit comments

Comments
 (0)