Leetcode数组
瑾明达2号
Happy cooperation
展开
-
1331. 数组序号转换
这个朋友写的非常棒:https://leetcode-cn.com/problems/rank-transform-of-an-array/solution/c-by-eric-345-63/ class Solution { public: //二分查找 int binary_search(vector<int>& arr, int num) { int left = 0; int right = arr.size() - 1;转载 2020-12-14 23:55:51 · 101 阅读 · 0 评论 -
1534. 统计好三元组
class Solution { public: int countGoodTriplets(vector<int>& arr, int a, int b, int c) { int ans = 0, n = arr.size(); for (int j = 1; j < n; j++) { for (int k = j + 1; k < n; k++) {原创 2020-12-14 23:16:16 · 54 阅读 · 0 评论 -
Leetcode 88. 合并两个有序数组
一般的想法是用一个新的数组,在这里新的数组就是从nums1的 m + n - 1位置开始作为一个新数组,只是这个数组的填充方式是逆序的。 class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i = m + n - 1; m--; n--; while原创 2020-11-22 22:28:02 · 41 阅读 · 0 评论 -
Leetcode 1275. 找出井字棋的获胜者
class Solution { public: string tictactoe(vector<vector<int>>& moves) { vector<vector<int>> cb = {{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}}; int size = moves.size(); cb[moves[0][0]][moves[0][1]] = 1;原创 2020-11-22 19:32:00 · 117 阅读 · 0 评论 -
Leetcode 832. 翻转图像
class Solution { public: vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) { int row = A.size(); int col = A[0].size(); vector<vector<int>> ret(row); for(int i =原创 2020-11-22 14:22:42 · 43 阅读 · 0 评论 -
Leetcode 605. 种花问题
class Solution { public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int count = 0; int size = flowerbed.size(); flowerbed.insert(flowerbed.begin(), 0); flowerbed.insert(flowerbed.end(), 0);原创 2020-11-22 13:39:22 · 93 阅读 · 0 评论 -
Leetcode 228. 汇总区间
class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> ret; int size = nums.size(); for (int i, j = 0; j < size; j++) { i = j; whi原创 2020-11-22 12:35:23 · 64 阅读 · 0 评论 -
Leetcode 219. 存在重复元素 II
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { int size = nums.size(); unordered_map<int, int> map; for (int i = 0; i < size; i++) { if (map.find原创 2020-11-22 00:30:09 · 43 阅读 · 0 评论 -
Leetcode 1550. 存在连续三个奇数的数组
class Solution { public: bool threeConsecutiveOdds(vector<int>& arr) { int count = 0; int size = arr.size(); for(int i = 0; i < size; i++) { if((arr[i] & 1) == 1) {原创 2020-11-21 20:54:52 · 60 阅读 · 0 评论 -
Leetcode 628. 三个数的最大乘积
class Solution { public: int maximumProduct(vector<int>& nums) { //假设max1 > max2 > max3 int max1 = INT_MIN; int max2 = INT_MIN; int max3 = INT_MIN; //假设min1 < min2 int min1 = INT_原创 2020-11-21 20:38:42 · 39 阅读 · 0 评论 -
Leetcode 1588. 所有奇数长度子数组的和
参考:https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/cong-on3-dao-on-de-jie-fa-by-liuyubobobo/ class Solution { public: int sumOddLengthSubarrays(vector<int>& arr) { int res = 0, left_even, right_even, lef原创 2020-11-21 18:57:28 · 105 阅读 · 0 评论 -
Leetcode 977. 有序数组的平方
class Solution { public: vector<int> sortedSquares(vector<int>& A) { int size = A.size(); int i = 0, j = 0; vector<int> ret, temp; for (int num: A) { temp.emplace_back(num.原创 2020-11-21 12:20:34 · 74 阅读 · 0 评论 -
Leetcode 面试题 17.10. 主要元素
class Solution { public: int majorityElement(vector<int>& nums) { // 摩尔投票算法 int major = 0; int count = 0; for(int i : nums) { if(count == 0) { major = i;原创 2020-11-21 10:15:01 · 74 阅读 · 0 评论 -
Leetcode数组 1343. 大小为 K 且平均值大于等于阈值的子数组数目
给你一个整数数组 arr 和两个整数 k 和 threshold 。 请你返回长度为 k 且平均值大于等于 threshold 的子数组数目。 示例 1: 输入:arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 输出:3 解释:子数组 [2,5,5],[5,5,5] 和 [5,5,8] 的平均值分别为 4,5 和 6 。其他长度为 3 的子数组的平均值都小于 4 (threshold 的值)。 示例 2: 输入:arr = [1,1,1,1,1], k = 1, t原创 2020-06-27 20:05:32 · 102 阅读 · 0 评论