【问题标题】:Trying to write the java code from the urlify problem in cracking the coding interview in python尝试从破解python中的编码面试中的urlify问题编写java代码
【发布时间】:2019-01-09 03:43:09
【问题描述】:

我从破解urlify问题(1.3)的编码面试中获取的Java代码:

URLify:编写一个方法,用 '%20' 替换字符串中的所有空格。您可以假设字符串末尾有足够的空间来容纳其他字符,并且您得到了字符串的“真实”长度。 (注意:如果用Java实现,请使用字符数组,以便您可以就地执行此操作。)

示例

输入:“约翰·史密斯先生,13 岁

输出:“%2eJohn%2eSmith 先生”

转换后的代码有一些问题。这是我的python代码:

def urlify(str, trueLength):
    spaceCount = 0
    index = 0
    i = 0
    for i in range(0, trueLength):
        if str[i] == ' ':
            spaceCount += 1
        print(spaceCount, "spaceCount")
    index = trueLength + spaceCount * 2
    if (trueLength < len(str)):
        str[trueLength] = '\0'
    for i in range(trueLength, 0):
        if str[i] == ' ':
            str[index - 1] = '0'
            str[index - 2] = '2'
            str[index - 3] = '%'
            index = index - 3
        else:
            str[index - 1] = str[i]
            index = index - 1


print(urlify("Mr John Smith     ", 13))

我认为问题之一是

str[trueLength] = '\0'

我不确定还有什么问题。我对这两行也有点困惑

if (trueLength < len(str)):
        str[trueLength] = '\0'

所以如果有人能解释这些台词,那就太棒了。我只是想完全了解盖尔的解决方案。


我找到的代码:

def urlify(string, length):
'''function replaces single spaces with %20 and removes trailing spaces'''
new_index = len(string)

for i in reversed(range(length)):
    if string[i] == ' ':
        # Replace spaces
        string[new_index - 3:new_index] = '%20'
        new_index -= 3
    else:
        # Move characters
        string[new_index - 1] = string[i]
        new_index -= 1

return string

【问题讨论】:

    标签: python arrays algorithm data-structures


    【解决方案1】:

    缩短代码(更 Pythonic 的方式):

    def urlify(string, real_length):
     return string[:real_length].replace(' ', '%20')
    

    解释:

    string[:real_length]
    # This limits strings to real length, in your case to 13. This will remove unnecessary end of the string. 
    .replace(' ', '%20')
    # This replaces every space with '%20'.
    

    关于您的代码:

    1. 在 Python 中,'str' 是保留字。不要使用它。

    2. 在 Python 中,您不能更改字符串。您必须创建一个新的。不支持字符串项分配。您的代码完全基于项目分配。您应该改为创建新字符串并向其添加字符。

    3. 这段代码真的搞砸了。这很难理解,你应该找到更简单的解决方案。

    您的代码和逻辑已优化:

    def urlify(string, trueLength):
        new_string = ''
        for i in range(0, trueLength):
            if string[i] == ' ':
                new_string=new_string+'%20'
            else:
                new_string=new_string+string[i]
        return new_string
    

    【讨论】:

    • 我认为这个问题的重要部分是使用列表,因为 python 字符串是不可变的
    • 您可以使用 Python 字符串,但您应该始终创建新字符串,而不是更改旧字符串。
    • 摘自“我们已经使用字符数组实现了这个问题,因为 Java 字符串是不可变的。如果我们直接使用字符串,函数将不得不返回字符串的新副本,但它允许我们只需一次完成即可。”所以也许最好完全避免创建一个新字符串
    • 我不了解 Java,但在 Python 中创建一个新字符串是非常好的。实际上,您必须创建一次新字符串 - 您不能更改旧字符串但必须返回字符串,这意味着您迟早必须创建一个新字符串。最好使用我的第一个示例(Pythonic 版本),但如果它对您/您的班级来说太高级了,那么您可以使用第二个解决方案。有不明白的地方欢迎追问,我来解释。
    • 另外,Python 中没有字符数组。是的,您可以使用字符串创建一个列表,但它不限于字符,更重要的是,它不是 Java 或 C 中的内置类型。这意味着使用它代替普通字符串不会有任何好处。
    【解决方案2】:

    我的猜测,只是试图理解你想理解的代码:

    注意:

    您将获得字符串的 "true" 长度。

    所以trueLength 可以比len(str) 短,您应该在代码中处理这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2019-09-29
      • 2020-09-20
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多