Skip to content

Commit 91b11d0

Browse files
committed
2024-11-08 v. 6.9.9: added "400. Nth Digit"
1 parent 40e22ac commit 91b11d0

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
564564
| 388. Longest Absolute File Path | [Link](https://leetcode.com/problems/longest-absolute-file-path/) | [Link](./lib/medium/388_longest_absolute_file_path.rb) | [Link](./test/medium/test_388_longest_absolute_file_path.rb) |
565565
| 394. Decode String | [Link](https://leetcode.com/problems/decode-string/) | [Link](./lib/medium/394_decode_string.rb) | [Link](./test/medium/test_394_decode_string.rb) |
566566
| 398. Random Pick Index | [Link](https://leetcode.com/problems/random-pick-index/) | [Link](./lib/medium/398_random_pick_index.rb) | [Link](./test/medium/test_398_random_pick_index.rb) |
567+
| 400. Nth Digit | [Link](https://leetcode.com/problems/nth-digit/) | [Link](./lib/medium/400_nth_digit.rb) | [Link](./test/medium/test_400_nth_digit.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.9.8.1'
8+
s.version = '6.9.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/400_nth_digit.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/nth-digit/
4+
# @param {Integer} n
5+
# @return {Integer}
6+
def find_nth_digit(n)
7+
sz = 1
8+
p = 1
9+
10+
loop do
11+
d = 9 * sz * p
12+
13+
return ((n - 1) / sz + p).to_s[(n - 1) % sz].to_i if n <= d
14+
15+
n -= d
16+
sz += 1
17+
p *= 10
18+
end
19+
end

test/medium/test_400_nth_digit.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/400_nth_digit'
5+
require 'minitest/autorun'
6+
7+
class NthDigitTest < ::Minitest::Test
8+
def test_default_one = assert_equal(3, find_nth_digit(3))
9+
10+
def test_default_two = assert_equal(0, find_nth_digit(11))
11+
end

0 commit comments

Comments
 (0)