【每日一题见微知著】力扣周赛又一次全AC,有点膨胀了

6016. Excel 表中某个范围内的单元格

Excel 表中的一个单元格 (r, c) 会以字符串 "<col><row>" 的形式进行表示,其中:

  • <col>
    

    即单元格的列号

    c
    

    。用英文字母表中的

    字母

    标识。

    • 例如,第 1 列用 'A' 表示,第 2 列用 'B' 表示,第 3 列用 'C' 表示,以此类推。
  • <row> 即单元格的行号 r 。第 r 行就用 整数 r 标识。

给你一个格式为 "<col1><row1>:<col2><row2>" 的字符串 s ,其中 <col1> 表示 c1 列,<row1> 表示 r1 行,<col2> 表示 c2 列,<row2> 表示 r2 行,并满足 r1 <= r2c1 <= c2

找出所有满足 r1 <= x <= r2c1 <= y <= c2 的单元格,并以列表形式返回。单元格应该按前面描述的格式用 字符串 表示,并以 非递减 顺序排列(先按列排,再按行排)。

class Solution {
    
    
    public List<String> cellsInRange(String s) {
    
    
        String[] b=s.split(":");
        char start=b[0].charAt(0);
        char end=b[1].charAt(0);
        int st=(b[0].charAt(1)-'0');
        int ed=(b[1].charAt(1)-'0');
        List<String> ans=new ArrayList<>();
        for(char i=start;i<=end;i++){
    
    
            for(int j=st;j<=ed;j++){
    
    
                StringBuffer res=new StringBuffer();
                res.append(i);
                res.append(j);
                ans.add(res.toString());
            }
        }
        return ans;
    }
}

6017. 向 数 组 中 追 加 K 个 整 数 \textcolor{orange}{6017. 向数组中追加 K 个整数} 6017.K

给你一个整数数组 nums 和一个整数 k 。请你向 nums 中追加 k 出现在 nums 中的、互不相同 整数,并使结果数组的元素和 最小

返回追加到 nums 中的 k 个整数之和。

class Solution {
    
    
    public long minimalKSum(int[] nums, int k) {
    
    
        long ans=0;
        Arrays.sort(nums);

        for(int i=0;i<nums.length;i++){
    
    
            if(i==0){
    
    
                if(k>(nums[0]-1)){
    
    
                    ans+=(long)(1+nums[0]-1)*(nums[0]-1)/2;
                    k-=(nums[0]-1);
                }
                else{
    
    
                    int end=k;
                    ans+=(long)(k+1)*k/2;
                    k=0;
                }
            }
            else{
    
    
                if(nums[i]!=nums[i-1]){
    
    
                    if(k>(nums[i]-nums[i-1]-1)){
    
    
                        int count=nums[i]-nums[i-1]-1;
                        ans+=(long)(nums[i]-1+nums[i-1]+1)*count/2;
                        k-=count;
                    }
                    else if(k!=0){
    
    
                        int end=nums[i-1]+k;
                        ans+=(long)(nums[i-1]+1+end)*k/2;
                        k=0;
                    }
                }
            }

        }

        if(k!=0){
    
    
            int end=nums[nums.length-1]+k;
            ans+=(long)(nums[nums.length-1]+1+end)*k/2;
            k=0;
        }
        return ans;
    }
}

6018. 根 据 描 述 创 建 二 叉 树 \textcolor{orange}{6018. 根据描述创建二叉树} 6018.

给你一个二维整数数组 descriptions ,其中 descriptions[i] = [parenti, childi, isLefti] 表示 parentichildi二叉树 中的 父节点,二叉树中各节点的值 互不相同 。此外:

  • 如果 isLefti == 1 ,那么 childi 就是 parenti 的左子节点。
  • 如果 isLefti == 0 ,那么 childi 就是 parenti 的右子节点。

请你根据 descriptions 的描述来构造二叉树并返回其 根节点

测试用例会保证可以构造出 有效 的二叉树。

class Solution {
    
    
    public TreeNode createBinaryTree(int[][] descriptions) {
    
    
        Map<Integer,int[]> e=new HashMap<>();
        int n=descriptions.length;
        Set<Integer> chSet=new HashSet<>();
        for(int i=0;i<n;i++){
    
    
            int fa=descriptions[i][0];
            int ch=descriptions[i][1];
            chSet.add(ch);
            int[] c=e.getOrDefault(fa,new int[]{
    
    0,0});
            if(descriptions[i][2]==1){
    
    
                c[0]=ch;
            }
            else{
    
    
                c[1]=ch;
            }
            e.put(fa,c);
        }
        int root=0;
        for(Map.Entry<Integer,int[]> mide:e.entrySet()){
    
    
            int fa=mide.getKey();
            if(!chSet.contains(fa)){
    
    
                root=fa;
                break;
            }
        }
        TreeNode res=new TreeNode(root);
        Deque<TreeNode> q=new ArrayDeque<>();
        q.add(res);
        while(!q.isEmpty()){
    
    
            TreeNode cur=q.pop();
            int[] ch=e.get(cur.val);
            if(ch==null){
    
    
                continue;
            }
            else{
    
    
                if(ch[0]!=0){
    
    
                    TreeNode left=new TreeNode(ch[0]);
                    cur.left=left;
                    q.add(left);
                }
                if(ch[1]!=0){
    
    
                    TreeNode right=new TreeNode(ch[1]);
                    cur.right=right;
                    q.add(right);
                }
            }
        }
        return res;
    }
}

6019. 替 换 数 组 中 的 非 互 质 数 \textcolor{red}{6019. 替换数组中的非互质数} 6019.

给你一个整数数组 nums 。请你对数组执行下述操作:

  1. nums 中找出 任意 两个 相邻非互质 数。
  2. 如果不存在这样的数,终止 这一过程。
  3. 否则,删除这两个数,并 替换 为它们的 最小公倍数(Least Common Multiple,LCM)。
  4. 只要还能找出两个相邻的非互质数就继续 重复 这一过程。

返回修改后得到的 最终 数组。可以证明的是,以 任意 顺序替换相邻的非互质数都可以得到相同的结果。

生成的测试用例可以保证最终数组中的值 小于或者等于 108

两个数字 xy 满足 非互质数 的条件是:GCD(x, y) > 1 ,其中 GCD(x, y)xy最大公约数

class Solution {
    
    
    public List<Integer> replaceNonCoprimes(int[] nums) {
    
    
        Stack<Long> stack=new Stack<>();
        long cur=nums[0];
        for(int i=1;i<nums.length;i++){
    
    
            long gcd=GCD(cur,nums[i]);
            if(gcd!=(long)1){
    
    
                cur=LCM(cur,nums[i],gcd);
            }
            else{
    
    
                while (!stack.isEmpty()&&GCD(cur,stack.peek())!=(long) 1){
    
    
                    long top=stack.pop();
                    long lcm=LCM(cur,top,GCD(cur,top));
                    cur=lcm;
                }
                stack.push(cur);
                cur=nums[i];
            }
        }
        while (!stack.isEmpty()&&GCD(cur,stack.peek())!=(long) 1){
    
    
            long top=stack.pop();
            long lcm=LCM(cur,top,GCD(cur,top));
            cur=lcm;
        }
        stack.push(cur);
        
        List<Integer> ans=new ArrayList<>();
        for(long i:stack){
    
    
            ans.add((int)i);
        }
        return ans;
    }

    public static long GCD(long m, long n) {
    
    
        long result = 0;
        while (n != 0) {
    
    
            result = m % n;
            m = n;
            n = result;
        }
        return m;
    }

    public static long LCM(long m,long n,long gcd){
    
    
        long res=0;
        res=(long)m*n;
        res/=gcd;
        return res;
    }
}

结尾

题目来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems

⭐️关注作者,带你刷题,从简单的算法题了解最常用的算法技能(寒假每日一题)
⭐️关注作者刷题——简单到进阶,让你不知不觉成为无情的刷题机器,有问题请私信

猜你喜欢

转载自blog.csdn.net/caqjeryy/article/details/123319267