Skip to content

Latest commit

 

History

History
38 lines (32 loc) · 845 Bytes

2011.执行操作后的变量值.md

File metadata and controls

38 lines (32 loc) · 845 Bytes

2011.执行操作后的变量值

/*
 * @lc app=leetcode.cn id=2011 lang=typescript
 *
 * [2011] 执行操作后的变量值
 */

// @lc code=start
function finalValueAfterOperations(operations: string[]): number {}
// @lc code=end

解法 1: 模拟

function finalValueAfterOperations(operations: string[]): number {
  let res = 0
  for (let s of operations) {
    if (s[0] === '+' || s[1] === '+') res++
    else res--
  }
  return res
}

Case

test.each([
  { input: { operations: ['--X', 'X++', 'X++'] }, output: 1 },
  { input: { operations: ['++X', '++X', 'X++'] }, output: 3 },
  { input: { operations: ['X++', '++X', '--X', 'X--'] }, output: 0 },
])('input: operations = $input.operations', ({ input: { operations }, output }) => {
  expect(finalValueAfterOperations(operations)).toEqual(output)
})