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 +```

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 +``` +

AltStyle によって変換されたページ (->オリジナル) /