From c4de753d6db1548205de14efdb4fc9194d7223c4 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 29 May 2021 08:07:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200020.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E6=8B=AC=E5=8F=B7=20Ruby=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 811fc316..4f4ec63a 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -336,6 +336,23 @@ var threeSum = function(nums) { ``` +ruby: +```ruby +def is_valid(strs) + symbol_map = {')' => '(', '}' => '{', ']' => '['} + stack = [] + strs.size.times {|i| + c = strs[i] + if symbol_map.has_key?(c) + top_e = stack.shift + return false if symbol_map[c] != top_e + else + stack.unshift(c) + end + } + stack.empty? +end +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From bd95f6a248f9175216582225500e3c074f0c4180 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 29 May 2021 09:12:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0=20Ruby=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 8da0fb89..f0f61d06 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -186,6 +186,20 @@ var removeElement = (nums, val) => { }; ``` +Ruby: +```ruby +def remove_element(nums, val) + i = 0 + nums.each_index do |j| + if nums[j] != val + nums[i] = nums[j] + i+=1 + end + end + i +end +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)