Skip to content

Commit b2c3430

Browse files
authored
Merge pull request #739 from fartem/227_Basic_Calculator_II
2024-10-01 v. 6.7.2: added "227. Basic Calculator II"
2 parents e1da825 + c7d0f14 commit b2c3430

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
537537
| 208. Implement Trie (Prefix Tree) | [Link](https://leetcode.com/problems/implement-trie-prefix-tree/) | [Link](./lib/medium/208_implement_trie_prefix_tree.rb) | [Link](./test/medium/test_208_implement_trie_prefix_tree.rb) |
538538
| 209. Minimum Size Subarray Sum | [Link](https://leetcode.com/problems/minimum-size-subarray-sum/) | [Link](./lib/medium/209_minimum_size_subarray_sum.rb) | [Link](./test/medium/test_209_minimum_size_subarray_sum.rb) |
539539
| 215. Kth Largest Element in an Array | [Link](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Link](./lib/medium/215_kth_largest_element_in_an_array.rb) | [Link](./test/medium/test_215_kth_largest_element_in_an_array.rb) |
540+
| 227. Basic Calculator II | [Link](https://leetcode.com/problems/basic-calculator-ii/) | [Link](./lib/medium/227_basic_calculator_ii.rb) | [Link](./test/medium/test_227_basic_calculator_ii.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.7.1'
8+
s.version = '6.7.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/basic-calculator-ii/
4+
# @param {String} s
5+
# @return {Integer}
6+
def calculate(s)
7+
stack = []
8+
curr = 0
9+
op = '+'
10+
11+
s.each_char do |c|
12+
if c.match?(/[0-9]/)
13+
curr = curr * 10 + c.to_i
14+
elsif c != ' '
15+
stack.push(
16+
perform_operation(
17+
stack,
18+
op,
19+
curr
20+
)
21+
)
22+
23+
curr = 0
24+
op = c
25+
end
26+
end
27+
28+
stack.push(
29+
perform_operation(
30+
stack,
31+
op,
32+
curr
33+
)
34+
)
35+
36+
stack.sum
37+
end
38+
39+
private
40+
41+
# @param {Array} stack
42+
# @param {String} op
43+
# @param {Integer} num
44+
# @return {Integer}
45+
def perform_operation(stack, op, num)
46+
case op
47+
when '+'
48+
num
49+
when '-'
50+
-num
51+
when '*'
52+
stack.pop * num
53+
when '/'
54+
(stack.pop.to_f / num).to_i
55+
end
56+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/227_basic_calculator_ii'
5+
require 'minitest/autorun'
6+
7+
class BasicCalculatorIITest < ::Minitest::Test
8+
def test_default_one = assert_equal(7, calculate('3+2*2'))
9+
10+
def test_default_two = assert_equal(1, calculate(' 3/2 '))
11+
12+
def test_default_three = assert_equal(5, calculate(' 3+5 / 2 '))
13+
14+
def test_additional_one = assert_equal(1, calculate('2-1'))
15+
end

0 commit comments

Comments
 (0)