首页 > 其他分享 >Leetcode每日一题 20240730 2961.双模幂运算

Leetcode每日一题 20240730 2961.双模幂运算

时间:2024-07-30 23:25:31浏览次数:15  
标签:10 下标 target 20240730 int variables ans 2961 Leetcode

题目描述

给你一个下标从 0 开始的二维数组 variables ,其中 variables[i] = [ai, bi, ci, mi],以及一个整数 target 。

如果满足以下公式,则下标 i 是 好下标:

  • 0 <= i < variables.length
  • ((aibi % 10)ci) % mi == target

返回一个由 好下标 组成的数组,顺序不限 。

2961.双模幂运算

测试案例及提示

示例 1:
输入:variables = [[2,3,3,10],[3,3,3,1],[6,1,1,4]], target = 2
输出:[0,2]
解释:对于 variables 数组中的每个下标 i :

  1. 对于下标 0 ,variables[0] = [2,3,3,10] ,(23 % 10)3 % 10 = 2 。
  2. 对于下标 1 ,variables[1] = [3,3,3,1] ,(33 % 10)3 % 1 = 0 。
  3. 对于下标 2 ,variables[2] = [6,1,1,4] ,(61 % 10)1 % 4 = 2 。
    因此,返回 [0,2] 作为答案。

示例 2:
输入:variables = [[39,3,1000,1000]], target = 17
输出:[]
解释:对于 variables 数组中的每个下标 i :

  1. 对于下标 0 ,variables[0] = [39,3,1000,1000] ,(393 % 10)1000 % 1000 = 1 。
    因此,返回 [] 作为答案。

提示:
1 <= variables.length <= 100
variables[i] == [ai, bi, ci, mi]
1 <= ai, bi, ci, mi <= 103
0 <= target <= 103

解题思路

读一遍题目下来,需要输出的是一个好下标组成的数组。
而一个好下标要满足两个条件,第二个条件涉及到幂和模运算。
在python中,提供了pow函数,可以同时进行幂运算和模运算。
在其他语言中,也可以通过使用快速幂的方法达到相近的效果。
python

class Solution:
    def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]:
        ans = []
        n = len(variables)
        for i, c in enumerate(variables):
            if pow(pow(c[0], c[1], 10), c[2], c[3]) == target:
                ans.append(i)
        return ans

C

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int powMod(int x, int y, int mod);
int* getGoodIndices(int** variables, int variablesSize, int* variablesColSize, int target, int* returnSize) {
    int* ans = (int *) malloc (variablesSize * sizeof(int));
    int j = 0;

    for (int i = 0; i < variablesSize; i++) {
        int a = variables[i][0];
        int b = variables[i][1];
        int c = variables[i][2];
        int m = variables[i][3];
        if (powMod(powMod(a, b, 10), c, m) == target) {
            ans[j] = i;
            j++;
        }
    }

    *returnSize = j;
    return ans;
}

int powMod(int x, int y, int mod) {
    int ans = 1;

    while (y > 0) {
        if (y & 1) {
            ans = ans * x % mod;
        }
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}

标签:10,下标,target,20240730,int,variables,ans,2961,Leetcode
From: https://blog.csdn.net/2301_76443687/article/details/140808805

相关文章

  • 代码随想录算法训练营Day0| LeetCode704: 二分查找
    LeetCode704二分查找先看了一下数组理论基础:数组基础题目链接:704.二分查找啥也没看,凭感觉直接上手:classSolution(object): defsearch(self,nums,target): fornuminnums: ifnum==target: returnnums.index(num) break return-1通过倒是......
  • (nice!!!)LeetCode 2952. 需要添加的硬币的最小数量(贪心、数组)
    题目:2952.需要添加的硬币的最小数量思路:假设区间[1,s-1]的数都可组合得到,当遍历到x=coins[i]时,1、当x<=s时,可以组合的数就是区间[1,s-1]和区间[x,s-1+x]的交集,即区间[1,s-1+x]2、当x>s时,区间[1,s-1]和区间[x,s-1+x]没有交集,那我就只能通过添加一个数来实现了。在这里......
  • 【调试笔记-20240730-Linux-OpenWrt 23.05 安装 Docker 配置 bitnami/Wordpress-with-
    调试笔记-系列文章目录调试笔记-20240730-Linux-OpenWrt23.05安装Docker配置bitnami/Wordpress-with-NGINX实现微信用户在线注册登录文章目录调试笔记-系列文章目录调试笔记-20240730-Linux-OpenWrt23.05安装Docker配置bitnami/Wordpress-with-NGINX实现......
  • leetcode题目总结
    前言本文为leetcode上的题目简单分析总结,仅作记录,欢迎提出建议,共同学习交流。 390.消除游戏列表 arr 由在范围 [1,n] 中的所有整数组成,并按严格递增排序。请你对 arr 应用下述算法:从左到右,删除第一个数字,然后每隔一个数字删除一个,直到到达列表末尾。重复上面的步......
  • LeetCode15 三数之和
    前言题目:15.三数之和文档:代码随想录——三数之和编程语言:C++解题状态:没思路…思路不可包含重复三元组的条件是本题最大的难点,本题的一大思路在与排序后进行去重。代码双指针法classSolution{public:vector<vector<int>>threeSum(vector<int>&nums......
  • LeetCode-day30-2961. 双模幂运算
    LeetCode-day30-2961.双模幂运算题目描述示例示例1:示例2:思路代码题目描述给你一个下标从0开始的二维数组variables,其中variables[i]=[ai,bi,ci,mi],以及一个整数target。如果满足以下公式,则下标i是好下标:0<=i<variables.length((aibi%10)ci)......
  • LeetCode 756. Pyramid Transition Matrix
    原题链接在这里:https://leetcode.com/problems/pyramid-transition-matrix/description/题目:Youarestackingblockstoformapyramid.Eachblockhasacolor,whichisrepresentedbyasingleletter.Eachrowofblockscontains onelessblock thantherowbenea......
  • LeetCode面试150——121买卖股票的最佳时机
    题目难度:简单默认优化目标:最小化平均时间复杂度。Python默认为Python3。目录1题目描述2题目解析3算法原理及程序实现3.1暴力求解3.2动态规划参考文献1题目描述给定一个数组prices,它的第i个元素prices[i]表示一支给定股票第i天的价格。你只能选择......
  • 代码随想录——完全平方数(Leetcode 279)
    题目链接动态规划动态规划思路:状态定义:定义一个一维数组dp,其中dp[i]表示组成整数i所需的最少完全平方数的数量。状态初始化:将dp数组中的所有元素初始化为Integer.MAX_VALUE,表示初始状态下组成每个整数的完全平方数数量是无限大(即不可能)。但dp[0]需要初始化为0,因为组成......
  • 《LeetCode热题100》---<双指针篇四道>
    本篇博客讲解LeetCode热题100道双指针篇中的第一道:移动零(简单)第二道:盛最多水的容器(中等)第一道:移动零(简单)classSolution{publicvoidmoveZeroes(int[]nums){for(intcur=0,dest=-1;cur<nums.length;cur++){//采用双指针......