LeetCode에서 Ruby를 공부해 보았습니다.
LeetCode란?
실제로 출제 된 기술 테스트를 풀 수있는 사이트
Tips
문제수가 많습니다만, Problems -> Lists -> Top 100 Liked Questions로 정렬할 수 있어 좋다. 본가의 해답이 준비되어 있지 않은 문제도 있습니다만, Youtube등으로 검색하면 히트 하는 것이 많습니다.
주의: 아래 답변은 불완전한 것을 포함합니다.
#1. TwoSum (easy)
Question:
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
nums[a] + nums[b] = 2 + 7 = target,
return [0, 1].
my_challenge.rb
def two_sum(nums, target)
nums.each_with_index do |num1, a|
nums.each_with_index do |num2, b|
return [a,b] if num1 + num2 == target
end
end
return "Cannot find two_sum"
end
puts two_sum([2, 6, 11, 15], 9)
#7. Reverse Integer (easy)
Question:
Given a 32-bit signed integer, reverse digits of an integer.
Example:
Input: 123
출력: 321
Input: -123
Output: -321
Input: 120
Output: 21
my_challenge.rb
# ignore solution for the case: 32bit
def reverse(x)
a = x.to_s.chars
b = []
i = -1
while i >= -a.length
b.push(a[i])
i -= 1
end
if(x > 0)
return b.join().to_i
else(x < 0)
return -b.join().to_i
end
end
#412. Fizz Buzz (easy)
Question:
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”.
For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
my_challenge.rb
def FizzBuzz(n)
i = 0
n.times do |i|
i += 1
if (i % 3 == 0 && i % 5 == 0)
puts "FizzBuzz"
elsif (i % 3 == 0)
puts "Fizz"
elsif (i % 5 == 0)
puts "Buzz"
else
puts i
end
end
end
#136. Single Number (easy)
Question:
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Example:
Input: [2,2,1]
Output: 1
Input: [4,1,2,1,2]
Output: 4
my_challenge.rb
def single_number(nums)
i = 0
a = []
while i < nums.length
if (a.include?(nums[i]))
a.delete(nums[i])
else
a.push(nums[i])
end
i += 1
end
puts a
end
#139. Word Break (medium)
Question:
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Example:
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
my_challenge.rb
#11 / 36 test cases passed.
def word_break(s, word_dict)
a = s.to_s.chars
b = []
i = 0
while i < s.length
b.push(a[0..i].join(''))
b.push(a[i+1..s.length].join(''))
i += 1
end
if (b.include?(word_dict[0] && word_dict[1]))
return true
end
end
Ruby Basics Memo
루프
#while
x = 0
while x<40
x+=1 #can't use ++,--
if (x%2) != 0
puts x
end
end
#for_in
nums = [21,54,32,6,4,23,5]
i = 0
for num in nums
i+=1
puts "##{i} is #{num}"
end
#each
nums.each do |num| #or (0..nums.length).each
puts "#{num} by another way"
end
#multi loop
array = [2, 7, 11, 15]
target = 9
array.each do |arr1, a|
array.each do |arr2, b|
return puts "#{arr1} and #{arr2}" if arr1 + arr2 == target
end
end
function
x = 25
def tow_nums(nums)
x = 10
return (nums[0] + nums[1]).to_s + " and the x is #{x}" # return x=10
end
puts tow_nums([23,234])
클래스
class Pokemon
attr_accessor :name, :hp, :attack
def initialize(name, hp, attack)
@name = name; @hp = hp; @attack = attack;
end
def bark
return "bowowow"
end
end
pikachu = Pokemon.new("pika","100","50")
puts pikachu.name #pika
puts pikachu.bark() #bowowow
Reference
이 문제에 관하여(LeetCode에서 Ruby를 공부해 보았습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/SotaMakino/items/db8c26b314fe0d48ad54텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)