首页 > 其他分享 >leetcode-1629-easy

leetcode-1629-easy

时间:2023-07-05 23:25:24浏览次数:46  
标签:code int 1629 number length numbers easy array leetcode

Slowest Key

You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.

To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.

If k > 0, replace the ith number with the sum of the next k numbers.
If k < 0, replace the ith number with the sum of the previous k numbers.
If k == 0, replace the ith number with 0.
As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].

Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!

Example 1:

Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]
Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
Example 2:

Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
Explanation: When k is zero, the numbers are replaced by 0. 
Example 3:

Input: code = [2,4,9,3], k = -2
Output: [12,5,6,13]
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
Constraints:

n == code.length
1 <= n <= 100
1 <= code[i] <= 100
-(n - 1) <= k <= n - 1

思路一:复制两遍数组,根据情况遍历即可

    public int[] decrypt(int[] code, int k) {
        int[] array = new int[code.length * 2];
        System.arraycopy(code, 0, array, 0, code.length);
        System.arraycopy(code, 0, array, code.length, code.length);

        if (k == 0) {
            Arrays.fill(code, 0);
        } else if (k > 0) {
            for (int i = 0; i < code.length; i++) {
                int sum = 0;
                for (int j = 1; j <= k; j++) {
                    sum += array[i + j];
                }
                code[i] = sum;
            }
        } else {
            for (int i = code.length; i < array.length; i++) {
                int sum = 0;

                for (int j = 1; j <= Math.abs(k); j++) {
                    sum += array[i - j];
                }

                code[i - code.length] = sum;
            }
        }

        return code;
    }

标签:code,int,1629,number,length,numbers,easy,array,leetcode
From: https://www.cnblogs.com/iyiluo/p/17530586.html

相关文章

  • leetcode-1652-easy
    DefusetheBombYouhaveabombtodefuse,andyourtimeisrunningout!Yourinformerwillprovideyouwithacirculararraycodeoflengthofnandakeyk.Todecryptthecode,youmustreplaceeverynumber.Allthenumbersarereplacedsimultaneously......
  • LeetCode 160. 相交链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*getIntersectionNode(ListNode*headA,ListNode*headB){......
  • 使用GoEasy快速实现Android原生app中的websocket消息推送
    摘要:GoEasy带来了一项令开发者振奋的消息:全面支持Android原生平台!现在,您可以在Android应用中使用最酷炫的实时通信功能,借助GoEasy轻松实现消息的发送和接收。本文将带您领略GoEasy最新版本的威力,为您的应用增添一抹鲜活的互动色彩。嗨,开发者朋友们!是时候展现您的技术才华,让您的A......
  • EasyUi的combobox使用方式
    1、如何显示出来<selectid="mySelect"></select>$('#mySelect').combobox();2、如何初始化数据productData.map(item=>{comboboxData.push({"id":item.staticDisplayValue,"text":item.staticDisplayValue,&q......
  • 【笔试实战】LeetCode题单刷题-编程基础 0 到 1【三】
    682. 棒球比赛题目链接682. 棒球比赛题目描述你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作......
  • [HFCTF 2021 Final]easyflask
    根据指引拿到源码,之前访问网页拿到的源码是无缩进的,我还以为是出题人故意这样,后来才知道view-source一下能看到有缩进的源代码。#!/usr/bin/python3.6importosimportpicklefrombase64importb64decodefromflaskimportFlask,request,render_template,sessionapp......
  • Leetcode155. 最小栈
    classMinStack{public:stack<int>st;multiset<int>s;MinStack(){}voidpush(intval){st.push(val);s.insert(val);}voidpop(){intval=st.top();st.pop();......
  • 图-邻接表-leetcode207
    你这个学期必须选修​​numCourses​​​门课程,记为​​0​​​到​​numCourses-1​​。在选修某些课程之前需要一些先修课程。先修课程按数组​​prerequisites​​给出,其中​​prerequisites[i]=[ai,bi]​​,表示如果要学习课程​​ai​​则必须先学习课程......
  • 图-邻接表-leetcode207
    你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses-1 。在选修某些课程之前需要一些先修课程。先修课程按数组 prerequisites 给出,其中 prerequisites[i]=[ai,bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。例如,先修课程对 [0,1] 表示:想要......
  • 浅析EasyCVR视频技术与AR实景智能管理平台在智慧厂区中的应用
    一、背景分析新型智慧厂区是运用人工智能、大数据、物联网和设备监控技术加强厂区安保和信息管理。通过先进技术,保障厂区生产运营安全,同时减少生产线上的人工干预、及时正确地采集各类生产数据,以及合理的生产计划编排与生产进度,并且整合各业务系统数据资源,构建一个高效智能、绿色......