• LeetCode每日一题——2562. Find the Array Concatenation Value


    一、题目

    You are given a 0-indexed integer array nums.

    The concatenation of two numbers is the number formed by concatenating their numerals.

    For example, the concatenation of 15, 49 is 1549.
    The concatenation value of nums is initially equal to 0. Perform this operation until nums becomes empty:

    If there exists more than one number in nums, pick the first element and last element in nums respectively and add the value of their concatenation to the concatenation value of nums, then delete the first and last element from nums.
    If one element exists, add its value to the concatenation value of nums, then delete it.
    Return the concatenation value of the nums.

    Example 1:

    Input: nums = [7,52,2,4]
    Output: 596
    Explanation: Before performing any operation, nums is [7,52,2,4] and concatenation value is 0.

    • In the first operation:
      We pick the first element, 7, and the last element, 4.
      Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74.
      Then we delete them from nums, so nums becomes equal to [52,2].
    • In the second operation:
      We pick the first element, 52, and the last element, 2.
      Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596.
      Then we delete them from the nums, so nums becomes empty.
      Since the concatenation value is 596 so the answer is 596.
      Example 2:

    Input: nums = [5,14,13,8,12]
    Output: 673
    Explanation: Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0.

    • In the first operation:
      We pick the first element, 5, and the last element, 12.
      Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512.
      Then we delete them from the nums, so nums becomes equal to [14,13,8].
    • In the second operation:
      We pick the first element, 14, and the last element, 8.
      Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660.
      Then we delete them from the nums, so nums becomes equal to [13].
    • In the third operation:
      nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673.
      Then we delete it from nums, so nums become empty.
      Since the concatenation value is 673 so the answer is 673.

    Constraints:

    1 <= nums.length <= 1000
    1 <= nums[i] <= 104

    二、题解

    我的垃圾解法

    class Solution {
    public:
        int digits(int num){
            int count = 0;
            while(num){
                num /= 10;
                count++;
            }
            return count;
        }
        long long findTheArrayConcVal(vector<int>& nums) {
            int n = nums.size();
            if(n == 0) return 0;
            else if(n == 1) return nums[0];
            int i = 0,j = n - 1;
            int tmp1 = nums[i], tmp2 = nums[j];
            int tmp = tmp1 * pow(10,digits(tmp2)) + tmp2;
            nums.erase(nums.begin() + j);
            nums.erase(nums.begin() + i);
            return tmp + findTheArrayConcVal(nums);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    官方题解的解法

    使用stoi函数和to_string函数求concatenation的值,更快捷

    class Solution {
    public:
        long long findTheArrayConcVal(vector<int>& nums) {
            long long ans = 0;
            for (int i = 0, j = nums.size() - 1; i <= j; i++, j--) {
                if (i != j) {
                    ans += stoi(to_string(nums[i]) + to_string(nums[j]));
                } else {
                    ans += nums[i];
                }
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    rk3128投影仪lcd显示四周显示不完整解决
    【Spring AOP】统一处理过程 代码实现
    云容灾最佳实践!美创DBRA助力包头医学院第二附属医院核心系统容灾建设
    Hadoop-Hive
    Unity可视化Shader工具ASE介绍——2、ASE的Shader创建和输入输出
    在十四届蓝桥杯开赛前一星期开始复习
    GitLab 知识树(三):gitlab指定版本安装
    vue项目打包_以生产环境prod模式打包_vue-cli-service 不是内部或外部命令,也不是可运行的程序---vue工作笔记0025
    ppt录屏制作微课,轻松打造精品课程
    js逆向基础篇-某音乐网站-xx音乐
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/133788480