【发布时间】:2021-03-21 07:09:42
【问题描述】:
我正在尝试在 Elixir 中解决 LeetCode 问题,因为没有大量资源可用于该语言的代码审查(尽管这可能会改变或者我可能错了)而且因为我来自 OOP 背景,所以我想我会继续在这里发帖。
我正在尝试使用 Elixir 解决 "first unique character in a string" LeetCode 问题,发现我的解决方案比我想象的要复杂,因为我不知道 Elixir 中的 Maps 的键自动按字母顺序排序,而不是按插入(虽然我可能是错的)。
我很想听听任何更简洁/更容易解决问题的解决方案。 FWIW,我打算把它们写在最后,以便其他人能够找到如何解决我自己找不到的问题的示例。
Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.
Examples:
s = "leetcode"
return 0.
s = "loveleetcode"
return 2.
Note: You may assume the string contains only lowercase English letters.
defmodule Algos do
def first_unique_char_index(str) do
arr = String.split(str, "", trim: true)
indexes = Enum.with_index(arr)
first = Enum.frequencies(arr)
|> Map.to_list
|> Enum.sort(fn ({a,_b}, {c,_d}) ->
{_char1, i1} = Enum.find(indexes, (fn {x,_i} -> x == a end))
{_char2, i2} = Enum.find(indexes, (fn {y,_j} -> y == c end))
i1 <= i2
end)
|> Enum.find(fn {_char, num} -> num == 1 end)
case first do
{char, _num} ->
result = Enum.find(indexes, fn {x, _i} -> char == x end)
{_letter, index} = result
index
nil ->
-1
end
end
end
Algos.first_unique_char_index("aabcc") # returns 2
Algos.first_unique_char_index("picadillo") # returns 0
Algos.first_unique_char_index("dood") # returns -1
【问题讨论】:
-
旁注: elixir中的
Maps不以任何方式排序。 -
我投票结束这个问题,因为这个问题属于codereview.stackexchange.com
-
@PawełObrok 虽然这可能是 CR 的主题,但在未来,请不要以 Code Review 站点的存在作为关闭问题的理由。评估请求并使用诸如需要更多关注(就像我在这里所做的那样)、主要基于意见等原因。然后您可以向 OP 提及它可以如果是 on-topic,请在 Code Review 上发布。请看Does being on-topic at another Stack Exchange site automatically make a question off-topic for Stack Overflow?
-
@SᴀᴍOnᴇᴌᴀ 将其标记为审核并请求转移到 codereview 是否有意义?
-
@PawelObrok 谢谢你的提问。请参阅this meta question 的答案。通常情况下,如果这里已经没有答案,我会说是,但该元数据的两个答案都指出“如果有答案,它们也都需要在代码审查中成为很好的答案。”坦率地说我不认为这里接受的答案是 CR 的好答案。
标签: algorithm functional-programming elixir