From 6167408cd29d884bb8b74cc73b05ce7b6aac87ed Mon Sep 17 00:00:00 2001 From: han Date: Wed, 9 Aug 2023 12:06:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A00018.=E5=9B=9B=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\225\260\344\271\213\345\222\214.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" index 28c20b7acd..17715b2e6f 100644 --- "a/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0018.\345\233\233\346\225\260\344\271\213\345\222\214.md" @@ -649,6 +649,54 @@ object Solution { } } ``` +### Ruby: + +```ruby +def four_sum(nums, target) + #结果集 + result = [] + nums = nums.sort! + + for i in 0..nums.size - 1 + return result if i > 0 && nums[i] > target && nums[i] >= 0 + #对a进行去重 + next if i > 0 && nums[i] == nums[i - 1] + + for j in i + 1..nums.size - 1 + break if nums[i] + nums[j] > target && nums[i] + nums[j] >= 0 + #对b进行去重 + next if j > i + 1 && nums[j] == nums[j - 1] + left = j + 1 + right = nums.size - 1 + while left < right + sum = nums[i] + nums[j] + nums[left] + nums[right] + if sum > target + right -= 1 + elsif sum < target + left += 1 + else + result << [nums[i], nums[j], nums[left], nums[right]] + + #对c进行去重 + while left < right && nums[left] == nums[left + 1] + left += 1 + end + + #对d进行去重 + while left < right && nums[right] == nums[right - 1] + right -= 1 + end + + right -= 1 + left += 1 + end + end + end + end + + return result +end +```

From 7ae878753b3d700d14a3bfd0c55194c618b62a03 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 10 Aug 2023 10:21:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A01047.=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\207\215\345\244\215\351\241\271.md" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" index ad54f0f88e..ffe1353039 100644 --- "a/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" +++ "b/problems/1047.\345\210\240\351\231\244\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\347\233\270\351\202\273\351\207\215\345\244\215\351\241\271.md" @@ -475,6 +475,26 @@ impl Solution { } ``` +### Ruby + +```ruby +def remove_duplicates(s) + #数组模拟栈 + stack = [] + s.each_char do |chr| + if stack.empty? + stack.push chr + else + head = stack.pop + #重新进栈 + stack.push head, chr if head != chr + end + end + + return stack.join +end +``` +