首页 > 其他分享 >LeetCode 974 Subarray Sums Divisible by K All In One

LeetCode 974 Subarray Sums Divisible by K All In One

时间:2024-06-10 21:34:06浏览次数:9  
标签:974 nums Divisible sum number let result Subarray remainder

LeetCode 974 Subarray Sums Divisible by K All In One

LeetCode 974 能被 K 整除的子数组之和

erros


function subarraysDivByK(nums: number[], k: number): number {
  // -5 / 0 / 5  
  let count: number = 0;
  // 单个元素
  for(let i = 0; i < nums.length; i++) {
    if(Math.abs(nums[i] % k) === 0) {
      // console.log(`✅ nums[i] =`, nums[i])
      count += 1;
    }
  }
  // 多个元素
  let i = 0;
  let j = 0;
  let remainder = 0;
  for(i; i < nums.length - 1; i++) {
    remainder = nums[i];
    // console.log(`✅ sum =`, sum)
    // reminder 提醒 ❌
    // remainder 余数 ✅
    for(j = i + 1; j < nums.length; j++) {
      remainder = (remainder + nums[j]) % k;
      // console.log(`❌ sum =`, sum)
      if(Math.abs(remainder) === 0) {
        count += 1;
      }
    }
  }
  return count;
};


/* 

Time Limit Exceeded
66 / 73 testcases passed

nums =
[0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0]

k =
10000

 */

// function subarraysDivByK(nums: number[], k: number): number {
//   // -5 / 0 / 5  
//   let count: number = 0;
//   // 单个元素
//   for(let i = 0; i < nums.length; i++) {
//     if(Math.abs(nums[i] % k) === 0) {
//       // console.log(`✅ nums[i] =`, nums[i])
//       count += 1;
//     }
//   }
//   // 多个元素
//   let i = 0;
//   let j = 0;
//   let sum = 0;
//   for(i; i < nums.length - 1; i++) {
//     sum = nums[i];
//     // console.log(`✅ sum =`, sum)
//     for(j = i + 1; j < nums.length; j++) {
//       sum += nums[j];
//       // console.log(`❌ sum =`, sum)
//       if(Math.abs(sum % k) === 0) {
//         count += 1;
//       }
//     }
//   }
//   return count;
// };


// function subarraysDivByK(nums: number[], k: number): number {
//   // -5 / 0 / 5  
//   let i = 0;
//   let j = 0;
//   let sum = 0;
//   let result = [];
//   let temp = [];
//   for(let i = 0; i < nums.length; i++) {
//     if(Math.abs(nums[i] % k) === 0) {
//       result.push([nums[i]]);
//     }
//   }
//   for(i; i < nums.length - 1; i++) {
//     // if(Math.abs(nums[i] % k) === 0) {
//     //   console.log(`✅ nums[i] =`, nums[i]);
//     //   result.push([nums[i]]);
//     //   console.log(`❓ result =`, result);
//     // }
//     temp = [];
//     j = i + 1;
//     temp.push(nums[i])
//     for(j; j < nums.length; j++) {
//       temp.push(nums[j])
//       sum = temp.reduce((s, item) => s += item, 0);
//       if(Math.abs(sum % k) === 0) {
//         result.push(temp);
//         // break;
//       }
//       console.log(`

标签:974,nums,Divisible,sum,number,let,result,Subarray,remainder
From: https://www.cnblogs.com/xgqfrms/p/18241060

相关文章

  • Leetcode 3171. Find Subarray With Bitwise AND Closest to K
    Leetcode3171.FindSubarrayWithBitwiseANDClosesttoK1.解题思路2.代码实现题目链接:3171.FindSubarrayWithBitwiseANDClosesttoK1.解题思路这道题坦率地说让我感觉很挫败,又一次没有自力搞定,是看了大佬们的答案才搞定的……知道比没有搞定更难受的......
  • 快充催NB群 974764414
    现在手机电池容量是按多少v计算啊,3.8v还是5v。比如说充入10wh,对应多少ah 3.85,满电4.35@天梦 “满电4.35”是什么意思@ 字面意思,充满4.35 @天梦 那官方宣传的5000mAh是按3.85v还是4.35v啊,比如说“小米13Ultra手机搭载5000mAh容量电池”  @天梦 那官方宣传的5000......
  • Codeforces 1974G Money Buys Less Happiness Now
    考虑到有一种贪心的思路就是能选就选。显然这是错的,因为可能存在后面更优的情况,即当\(c_i>c_j(i<j)\)时,选\(j\)肯定比选\(i\)更优,因为后面剩下的更多且中间也留下了一些。于是考虑反悔贪心。还是一样的,如果能选就一定选上。否则来说,考虑对于当前已经选了的中的最大......
  • 152- Maximum Produce Subarray-最大子数组之乘积
    问题描述Givenanintegerarray nums,finda subarray.thathasthelargestproduct,andreturn theproduct.Thetestcasesaregeneratedsothattheanswerwillfitina 32-bit integer.解释:找出一个数组中乘积最大的子数组,返回子数组的乘积。案例:Input......
  • LeetCode 918. Maximum Sum Circular Subarray
    原题链接在这里:https://leetcode.com/problems/maximum-sum-circular-subarray/description/题目:Givena circularintegerarray nums oflength n,return themaximumpossiblesumofanon-empty subarray of nums.A circulararray meanstheendofthearray......
  • CF1965D Missing Subarray Sum题解
    题目链接点击打开链接题目解法感觉一点都不好想\fn因为最后的序列\(a\)是回文的,所以只有以中心点对称的子段和出现次数为奇数,其他都为偶数考虑有没有什么知道所有子段和的做法,可以方便推出缺失一个的答案令\(g_{l,r}\)为\(l\)到\(r\)的子段和知道\(g_{1,n}\)删......
  • 53_Maximum Subarray-最大子数组
    问题描述Givenanintegerarray nums,findthe subarray withthelargestsum,andreturn itssum.给定一个数组nums,找到一个子数组。使它的和最大,返回子数组例子Input:nums=[-2,1,-3,4,-1,2,1,-5,4]Output:6Explanation:子数组[4,-1,2,1]有最大的和6.基......
  • LeetCode 1186. Maximum Subarray Sum with One Deletion
    原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/题目:Givenanarrayofintegers,returnthemaximumsumfora non-empty subarray(contiguouselements)withatmostoneelementdeletion. Inotherwords,youwa......
  • [53] Maximum Subarray
    算法助手用户:这题应该怎么做?Givenanintegerarraynums,findthesubarraywiththelargestsum,andreturnitssum.ChatGPT:这个问题是一个非常经典的算法问题,被称为最大子数组和问题,可以通过动态规划(DynamicProgramming)的方法高效解决。我们可以使用一个名为“Kadan......
  • P9745 「KDOI-06-S」树上异或
    P9745「KDOI-06-S」树上异或位运算trick+树形dp看到题目中贡献的计算,可以想到乘法分配律,也就是一个连通块的乘积可以直接乘在当前所有方案的权值之和上。可以考虑特殊性质:链。那么树的问题就变成了序列问题。容易设\(f_i\)表示\(i\)以前的节点的所有断边方案权值和。转移......