全部评论
我的代码,供参考https://www.nowcoder.com/discuss/261441?toCommentId=4082824
第一题 #include <iostream>
using namespace std;
const int MAXN = 1010;
int A[MAXN][MAXN];
/*
3 3
1 2 3
4 5 6
7 8 9
*/
int main () {
int N, M;
cin >> N >> M;
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
cin >> A[i][j];
int sr = 0, sc = 0;
int tr = N - 1, tc = M - 1;
int cnt = 0;
while (cnt < M * N) {
// cout << sr << ", " << sc << endl;
// cout << tr << ", " << tc << endl;
for (int r = sr; r <= tr; ++r) {
cout << A[r][sc] << " ";
cnt++;
}
for (int c = sc + 1; c <= tc; ++c) {
cout << A[tr][c] << " ";
cnt++;
}
if (sr < tr && sc < tc) {
for (int r = tr - 1; r >= sr; --r) {
cout << A[r][tc] << " ";
cnt++;
}
for (int c = tc - 1; c > sc; --c) {
cout << A[sr][c] << " ";
cnt++;
}
}
sr++, sc++;
tr--, tc--;
}
return 0;
}
第二题我用的二分查找O(nlogn)也过了,代码: #include <iostream>
using namespace std;
const int MAXN = int(2e6 + 10);
int A[MAXN];
int preSum[MAXN];
/*
3 3
1 2 3
4 5 6
7 8 9
*/
int lower_bound(int p[], int l, int r, int target) {
while (l < r) {
int m = l + (r - l) / 2;
if (p[m] >= target) {
r = m;
} else {
l = m + 1;
}
}
return l;
}
int main () {
int n, s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
cin >> A[i];
if (i == 0) preSum[i] = A[i];
else preSum[i] = preSum[i - 1] + A[i];
}
// for (int i = 0; i < n; ++i) {
// cout << preSum[i] << " ";
// }
int max_len = (A[0] <= s);
for (int i = 1; i < n; ++i) {
// Sum([i, j]) = p[j] - p[i - 1] <= s
// p[i - 1] >= p[j] - s
int left = lower_bound(preSum, -1, i, preSum[i] - s);
max_len = max(max_len, i - left);
}
cout << max_len << endl;
return 0;
}
感觉第三题这种题目……光是处理输入就麻烦死了,哎,为啥不能像leetcode一样处理干净输入输出……
为什么Js不能平时那样写个函数体就行了,这个输入真的搞的我心态炸了
第二天链表加双指针,全过了。第三题没时间😭
第三题 15 直接排序,,,
第3题直接按照字典序排序,加上一点随机操作,过了40%。。。
let n = 3;
let m = 4;
let flag = true;
let num = 1;
let workSpace = [];
let outArr = [];
for (let i = 0; i < n; i++) {
workSpace[i] = [];
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
workSpace[i][j] = num++;
}
}
console.log(workSpace);
function check(flag, n, m) {
if (n == 0) {
return;
} else if (m == 0) {
} else if (flag) {
for (let i = 0; i < n; i++) {
outArr.push(workSpace[i][0]);
workSpace[i].shift();
if (i === n - 1) {
for (let j = 0; j < m - 1; j++) {
outArr.push(workSpace[i][j]);
}
}
}
workSpace.pop();
check(!flag, n - 1, m - 1);
} else {
for (let i = n - 1; i >= 0; i--) {
outArr.push(workSpace[i][m - 1]);
workSpace[i].pop();
if (i === 0) {
for (let j = m - 2; j >= 0; j--) {
outArr.push(workSpace[i][j]);
}
}
}
workSpace.shift();
check(!flag, n - 1, m - 1);
}
}
check(flag, n, m);
console.log(outArr); 这个第一题,啥原因啊,
话说起来,人均两个的话……岂不是没戏了……第三题不给分估计不成
给我发错了笔试邮箱,错过了
感觉人均 2 个啊,第三题是个锤子,输入输出都写了半天🤣
第一题只有15%……leetcode原题目是顺时针,你是怎么变为逆时针的。我直接把矩阵转置,提醒超时
第二题树状数组或者线段树应该能做,最后没时间了
第三道题例子没过,提交之后40%....
第二题看错题了,没看到连续,改了半天。。然后剩30min看最后一题,完全没心思做了
🤣还剩25分钟看了第三题,直接提交了 第一题一直95%,没懂什么情况
😂我也是,一看第三题说很难调,看了一下感觉确实难调,直接提交了
蹲题面
求答案
相关推荐
06-25 18:32
门头沟学院 运营 点赞 评论 收藏
分享
点赞 评论 收藏
分享