diff --git a/README.md b/README.md index ff522923..519a801c 100644 --- a/README.md +++ b/README.md @@ -134,3 +134,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 643. Maximum Average Subarray I | [Link](https://leetcode.com/problems/maximum-average-subarray-i/) | [Link](./lib/easy/643_maximum_average_subarray_i.rb) | | 645. Set Mismatch | [Link](https://leetcode.com/problems/set-mismatch/) | [Link](./lib/easy/645_set_mismatch.rb) | | 653. Two Sum IV - Input is a BST | [Link](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | [Link](./lib/easy/653_two_sum_iv_input_is_a_bst.rb) | +| 657. Robot Return to Origin | [Link](https://leetcode.com/problems/robot-return-to-origin/) | [Link](./lib/easy/657_robot_return_to_origin.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index bcdb8ed0..f7b2e65c 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '1.2.8' + s.version = '1.2.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE] s.executable = 'leetcode-ruby' diff --git a/lib/easy/657_robot_return_to_origin.rb b/lib/easy/657_robot_return_to_origin.rb new file mode 100644 index 00000000..ef9fb109 --- /dev/null +++ b/lib/easy/657_robot_return_to_origin.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/robot-return-to-origin/ +# @param {String} moves +# @return {Boolean} +def judge_circle(moves) + hor = 0 + vert = 0 + (0..moves.length).each do |i| + case moves[i] + when 'U' + vert += 1 + when 'D' + vert -= 1 + when 'L' + hor += 1 + when 'R' + hor -= 1 + end + end + + hor.zero? && vert.zero? +end diff --git a/test/easy/test_657_robot_return_to_origin.rb b/test/easy/test_657_robot_return_to_origin.rb new file mode 100644 index 00000000..5cb222fd --- /dev/null +++ b/test/easy/test_657_robot_return_to_origin.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/easy/657_robot_return_to_origin' +require 'minitest/autorun' + +class RobotReturnToOriginTest < ::Minitest::Test + def test_default + assert(judge_circle('UD')) + assert(!judge_circle('LL')) + end + + def test_additional + assert(judge_circle('RL')) + end +end