Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the code review

  1. The code given is just a single line of code- "test". It doesn't seem to be a valid code.
  2. It looks like the code is trying to add something, but it's not clear what it is and how it works.
  3. It is possible that there could be bugs in this code if it is not properly tested, so testing should be done before deploying the code.
  4. To improve the code, it might be helpful to provide more context about what it is trying to do and how it works. This will make it easier for others to understand and review the code.

104 changes: 104 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// Created by Pointer Null on 2023/3/20.
//
//给你一个长度为 n 的数组 nums ,该数组由从 1 到 n 的 不同 整数组成。另给你一个正整数 k 。
//
// 统计并返回 nums 中的 中位数 等于 k 的非空子数组的数目。
//
// 注意:
//
//
// 数组的中位数是按 递增 顺序排列后位于 中间 的那个元素,如果数组长度为偶数,则中位数是位于中间靠 左 的那个元素。
//
//
//
// 例如,[2,3,1,4] 的中位数是 2 ,[8,4,3,5,1] 的中位数是 4 。
//
//
// 子数组是数组中的一个连续部分。
//
//
//
//
// 示例 1:
//
//
//输入:nums = [3,2,1,4,5], k = 4
//输出:3
//解释:中位数等于 4 的子数组有:[4]、[4,5] 和 [1,4,5] 。
//
//
// 示例 2:
//
//
//输入:nums = [2,3,1], k = 3
//输出:1
//解释:[3] 是唯一一个中位数等于 3 的子数组。
//
//
//
//
// 提示:
//
//
// n == nums.length
// 1 <= n <= 10⁵
// 1 <= nums[i], k <= n
// nums 中的整数互不相同
//
//
// Related Topics 数组 哈希表 前缀和 👍 93 👎 0


//leetcode submit region begin(Prohibit modification and deletion)
#include "vector"
#include "map"
using namespace std;

class Solution {
public:
int find_1(vector<int>arr1,vector<int>arr2,int idx){
int sum=0;
for (int i = arr2.size()-1; i >=0 ; --i) {
for (int j = 0; j <arr1.size() ; ++j) {
if (arr2[i]>idx&&arr1[j]<idx&&(arr2[i]-arr1[j]-1)%2==0){
sum++;
} else return sum;
}
}
return sum;
}
int find_0(vector<int>arr,int idx){
int sum=0;
for (int i = arr.size()-1;i>=0; --i){
if (arr[i]>idx){
for (int j = 0; j < i; ++j) {
if (arr[j]<idx){
if ((arr[i]-arr[j]-1)%2!=0)sum++;
} else break;
}
} else return sum;
}
return sum;
}
int countSubarrays(vector<int>& nums, int k) {
vector<int>count(nums.size()+1,0);
map<int,vector<int>>F;
int index=-1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i]>k){
count[i+1]=count[i-1]+1;
} else if (nums[i]<k){
count[i+1]=count[i-1]-1;
} else {
index=i;
}
F[count[i+1]].push_back(i);
}
for (auto arr:F) {

}
return 0;
}
};
//leetcode submit region end(Prohibit modification and deletion)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code review

  1. The class Solution should be public, and the function countSubarrays should be declared as public.
  2. The function find_1 and find_0 should have a return type of int.
  3. The code should use proper indentation and spacing.
  4. In the function countSubarrays, it should use a for loop to iterate the nums vector instead of using the map data structure.
  5. The for loops in the functions find_1 and find_0 should use the size()-1 instead of the size().
  6. The code should use descriptive variables names for better understanding.
  7. There should be a check for an empty array.
  8. The code should have comments describing the purpose of each line of code.