当前位置: 首页 > news >正文

【每日一题Day65】LC2011执行操作后的变量值 | 模拟

执行操作后的变量值【LC2011】

There is a programming language with only four operations and one variable X:

  • ++X and X++ increments the value of the variable X by 1.
  • --X and X-- decrements the value of the variable X by 1.

Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

再去练几道状态压缩dp!

  • 思路:遍历整个数组,模拟加一和减一的操作,最后返回结果。进行加一或者减一的操作取决于字符串的第二个字符:

    • 如果第二个字符是+号,那么执行加一操作
    • 如果第二个字符是-号,那么执行减一操作
  • 实现

    class Solution {
        public int finalValueAfterOperations(String[] operations) {
            int res = 0;
            for (String operation : operations){
                res += operation.charAt(1) == '+' ? 1 : -1;
            }
            return res;
        }
    }
    
    • 复杂度
      • 时间复杂度: O ( n ) O(n) O(n), n n n为数组长度
      • 空间复杂度: O ( 1 ) O(1) O(1)

相关文章:

  • 小学生C++编程基础 课程8(B)
  • 阿里云将加速与伙伴合作 促进Web3.0生态发展
  • 如何用DWDM射频光纤技术实现200公里外的站点分集
  • 跨孔CT交叉梯度联合反演算法
  • Python获取与处理文件路径/目录路径
  • K8s调度之污点与容忍
  • @SuppressWarnings使用-屏蔽一些无关紧要的警告
  • 算法leetcode|25. K 个一组翻转链表(rust重拳出击)
  • GEO芯片数据基本分析
  • Logoist - 适用于设计师以及初次使用者,快速制作精美 logo
  • 有趣的网站分享——福音戰士標題生成器
  • 现在的时代不是互联网时代的延续,因为其底层逻辑已经改变
  • 智能工厂的关键技术
  • SwiftUI获取子View的frame
  • C++-std:tuple元组的基本用法
  • 【SpringMVC】SpringMVC的入门
  • TensorFlow性能分析调研
  • 四、网络层(二)路由算法与路由选择协议
  • Shiro与SpringBoot整合
  • Netflix Eureka 2.0.0正式发布:借尸还魂还是虚晃一枪?