LeetCode(String) 2011. Final Value of Variable After Performing Operations
1.问题
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.
Example 1:
Input: operations = [“–X”,“X++”,“X++”]
Output: 1
Explanation: The operations are performed as follows:
Initially, X = 0.
–X: X is decremented by 1, X = 0 - 1 = -1.
X++: X is incremented by 1, X = -1 + 1 = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
Example 2:
Input: operations = [“++X”,“++X”,“X++”]
Output: 3
Explanation: The operations are performed as follows:
Initially, X = 0.
++X: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
X++: X is incremented by 1, X = 2 + 1 = 3.
Example 3:
Input: operations = [“X++”,“++X”,“–X”,“X–”]
Output: 0
Explanation: The operations are performed as follows:
Initially, X = 0.
X++: X is incremented by 1, X = 0 + 1 = 1.
++X: X is incremented by 1, X = 1 + 1 = 2.
–X: X is decremented by 1, X = 2 - 1 = 1.
X–: X is decremented by 1, X = 1 - 1 = 0.
2.解决思路
方法1:
1.判断数据是存在,不存在返回0
2.设置一个int[]temp数组,长度和String[] operations的长度相同
3.将String[] operations的元素遍历,进行判断并赋值为整数的值放入temp数组中
4.遍历int[]temp的元素进行求和
方法2:
1.定义一个变量res,初始值为0
2.遍历String[] operations,用contains() Method如果元素包含“+”,在res上加一,否则减-
3.返回res值
3.代码实现
代码1:
class Solution {public int finalValueAfterOperations(String[] operations) {//1.判断数据是存在,不存在返回0if(operations == null){return 0;}//2.设置一个int[]temp数组,长度和String[] operations的长度相同int[] temp = new int[operations.length]; //3.将String[] operations的元素遍历,进行判断并赋值为整数的值放入temp数组中for(int i=0;i<operations.length;i++){if(operations[i].equals("X++") || operations[i].equals("++X")){temp[i] = 1;}else{temp[i] = -1;}}//4.遍历int[]temp的元素进行求和int sum=0;for(int j=0;j<temp.length;j++){sum += temp[j];}return sum; }
}
代码2
class Solution {public int finalValueAfterOperations(String[] operations) {//1.定义一个变量res,初始值为0int res = 0;//2.遍历String[] operations,用contains() Method如果元素包含“+”,在res上加一,否则减-for(int i = 0; i < operations.length; i++){if(operations[i].contains("+"))res++;elseres--;}//3.返回res值return res; }
}
LeetCode(String) 2011. Final Value of Variable After Performing Operations相关推荐
- LeetCode(String) 2325. Decode the Message
1.问题 You are given the strings key and message, which represent a cipher key and a secret message, r ...
- LeetCode(String)1768. Merge Strings Alternately
1.问题 You are given two strings word1 and word2. Merge the strings by adding letters in alternating o ...
- LeetCode (12.整数转罗马数字)JAVA StringBuffer
LeetCode (12.整数转罗马数字)JAVA StringBuffer 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 1 ...
- LeetCode(13.罗马数字转整数) JAVA Hashmap
LeetCode(13.罗马数字转整数) JAVA Hashmap 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...
- 浅谈 Java 字符串(String, StringBuffer, StringBuilder)
我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String ...
- 字符串的驻留(String Interning)
关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大一部分人对它存在迷惑.在开始关于字符串的驻留之前,先给出一个有趣的Sample: Code Snip: static vo ...
- Redis和nosql简介,api调用;Redis数据功能(String类型的数据处理);List数据结构(及Java调用处理);Hash数据结构;Set数据结构功能;sortedSet(有序集合)数
1.Redis和nosql简介,api调用 14.1/ nosql介绍 NoSQL:一类新出现的数据库(not only sql),它的特点: 1. 不支持SQL语法 2. 存储结构跟传统关系型数 ...
- JavaSE——面向对象高级(继承、final关键字、抽象类与接口、多态、Object类、内部类、包装类、可变参数)
第3节 面向对象高级 一.继承 1.1 概述和使用 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类.继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法 ...
- 当面试官问我————为什么String是final的?
面试官:你好,能看得清下面这张图吗? 我:可以的. 面试官:恩,好的.呃,你能不能说一说为什么String要用final修饰? 我:final意味着不能被继承或者被重写,String类用final修饰 ...
最新文章
- C++学习笔记13-类继承
- k8s 关键字以及管理流程。
- cent0S7根分区扩容以及问题解决
- python mysql数据库操作grid控件_Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能...
- minicom/picocom/cutecom/putty 安装与使用教程
- mysql 与 redis 如何保证数据一致性问题 ?
- 【Flink】ExceptionInChainedOperatorException: Could not forward element to next operator Buffer
- SpringCloud工作笔记046---PostMan打不开怎么解决
- MapReduce进程
- hpdl388安装2012系统_2010、2012型双端面中压釜用机械密封-安装指导
- 16进制颜色与UIColor互转
- 如何解决Mac使用向日葵(sunlogin)经常无法打开 connect is error
- 一年中所有节日的排列顺序_我国一年中的传统节日。(按顺序)
- 【文末彩蛋】国产PLC通信实例
- cf Educational Codeforces Round 54 C. Meme Problem
- 原创安卓手机QQ7.0登录界面动态背景视频实现方案
- AD18安装及其中英文切换
- 语音识别 --- 音频信号提取
- 基于MQ-135传感器和Arduino开发板的烟雾探测器
- unity中控制游戏物体移动最基本的三种方法