From bd95f6a248f9175216582225500e3c074f0c4180 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 29 May 2021 09:12:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=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)