擅长:python、mysql、java
<blockquote>
<p>I would rather like to avoid having to do a O(n)</p>
</blockquote>
<p>如果这是一个算法作业,我建议你不要读这个解决方案,除非你自己弄明白。。。但是如果不是,那么我建议不要使用split()函数来执行您自己的分割。。。这样做的方式就是在字符串中逐字符迭代,然后手动创建输出列表</p>
<pre><code>index = 0
output_array = ['']
for character in string:
if character != '/':
if output_array[index] == None:
output_array[index] = ''
output_array[index] += character
else if output_array[index] != '': # character == '/'
index++
</code></pre>
<p>上面的代码只是一个伪代码,所以你必须自己重写它。。。此外,此解决方案的问题是,如果<code>path</code>变量中没有任何内容,它将输出<code>['']</code>,但这是一个很容易解决的问题:)</p>