我正在格式化电话号码,短划线后不能有一位数字,即123-555-5555
或12-34
,但不能是123-4
。答案中也可以包含任何字母字符。
Here是我的答案。
class CodeTestException < Exception; end
# driver method
def phone_format(s)
string = s.to_s.gsub(/[^0-9]/, '') # force input to string
string_length = string.length
# ensure that the return type stays consistent and make sure that there isn't
# one digit by itself as specified by the API
return string unless string_length > 2
format_phone_string(string, string_length)
end
private
def format_phone_string(string, string_length)
formatted_string = ""
early_dash = string_length % 3 == 1
skip_dash = false
# start index at 1 for easier comprehension
i = 0
string.each_char do |char|
formatted_string << char
break if i == string_length
i += 1
formatted_string << '-' && skip_dash = true if early_dash && (i == string_length - 2)
formatted_string << '-' if i % 3 == 0 && !skip_dash
end
formatted_string
end
raise CodeTestException unless phone_format("(+1) 888 33x19") == "188-833-19"
raise CodeTestException unless phone_format("555 123 1234") == "555-123-12-34"
raise CodeTestException unless phone_format("(+1)") == '1'
raise CodeTestException unless phone_format(nil) == ""
raise CodeTestException unless phone_format("") == ""
有人告诉我答案不是递归或扫描。我相信O(n)
是我可以度过的最佳时间。我发现其他一些解决方案是线性时间的较高倍数。谁能击败我的解决方案?