File tree Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -490,3 +490,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
490
490
| 59. Spiral Matrix II | [ Link] ( https://leetcode.com/problems/spiral-matrix-ii/ ) | [ Link] ( ./lib/medium/59_spiral_matrix_ii.rb ) |
491
491
| 61. Rotate List | [ Link] ( https://leetcode.com/problems/rotate-list/ ) | [ Link] ( ./lib/medium/61_rotate_list.rb ) |
492
492
| 62. Unique Paths | [ Link] ( https://leetcode.com/problems/unique-paths/ ) | [ Link] ( ./lib/medium/62_unique_paths.rb ) |
493
+ | 71. Simplify Path | [ Link] ( https://leetcode.com/problems/simplify-path/ ) | [ Link] ( ./lib/medium/71_simplify_path.rb ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.2.4 '
8
+ s . version = '6.2.5 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # https://leetcode.com/problems/simplify-path/
4
+ # @param {String} path
5
+ # @return {String}
6
+ def simplify_path ( path )
7
+ stack = [ ]
8
+ s = path . split ( '/' )
9
+
10
+ s . each do |str |
11
+ if str == '..'
12
+ stack . pop unless stack . empty?
13
+ elsif str != '.' && !str . empty? && str != '..'
14
+ stack << "/#{ str } "
15
+ end
16
+ end
17
+
18
+ return '/' if stack . empty?
19
+
20
+ stack . join
21
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../test_helper'
4
+ require_relative '../../lib/medium/71_simplify_path'
5
+ require 'minitest/autorun'
6
+
7
+ class SimplifyPathTest < ::Minitest ::Test
8
+ def test_default
9
+ assert_equal ( '/home' , simplify_path ( '/home/' ) )
10
+ assert_equal ( '/home/foo' , simplify_path ( '/home//foo/' ) )
11
+ assert_equal ( '/home/user/Pictures' , simplify_path ( '/home/user/Documents/../Pictures' ) )
12
+ assert_equal ( '/' , simplify_path ( '/../' ) )
13
+ assert_equal ( '/.../b/d' , simplify_path ( '/.../a/../b/c/../d/./' ) )
14
+ end
15
+ end
You can’t perform that action at this time.
0 commit comments