查看: 658| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 以#953 为例,请教各位面试官,我该选择短代码还是容易讲明白思路但是更长的代码

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 只是Leon 于 2020-12-17 12:13 编辑

今天刷#953 想到一个问题,我发现按照自己的思路很容易就用到子函数,就想问一下面试官如果你面对两个答案,假设都是正确的,哪个会让你更加心动💓 ?

附上栗子一枚
953. Verifying an Alien Dictionary具体题目见代码后
代码1,优点:短,缺点:我自己发现讲明白逻辑比较难(当然我明白有的大神能够把这个短代码的逻辑也讲得很清楚,但是我在很多栗子里面已经发现我不具备这样的能力)
  1. class Solution:
  2.     def isAlienSorted(self, words: List[str], order: str) -> bool:
  3.         
  4.         for i in range(len(words) -1):
  5.             word1 = words[i]
  6.             word2 = words[i+1]
  7.             
  8.             for j in range(min(len(word1), len(word2))):
  9.                 if word1[j] == word2[j]:
  10.                     continue
  11.                 elif order.index(word1[j]) < order.index(word2[j]):
  12.                     break
  13.                 else:
  14.                     return False
  15.             else:
  16.                 if len(word1) > len(word2):
  17.                     return False
  18.                 continue

  19.         return True
复制代码



代码2,优点:我自己讲解起来思路非常清晰,缺点:长。。。。。。
  1. class Solution:
  2.     def isAlienSorted(self, words: List[str], order: str) -> bool:
  3.         
  4.         for i in range(len(words) -1):
  5.             if self.word1_smaller(words[i], words[i+1], order):
  6.                 continue
  7.             else:
  8.                 return False

  9.         return True # reaching this statement means no words is greater than the right
  10.         
  11.     def word1_smaller(self, word1: str, word2: str, order: str) -> bool:
  12.         
  13.         for j in range(min(len(word1), len(word2))):
  14.             if word1[j] == word2[j]:
  15.                 continue
  16.             elif order.index(word1[j]) < order.index(word2[j]):
  17.                 return True
  18.             else:
  19.                 return False
  20.         else:
  21.             if len(word1) <= len(word2):
  22.                 return True
  23.             else:
  24.                 return False
复制代码



问题详细说明
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.



Example 1:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).

[/i][/i]

上一篇:leetcode的刷题顺序有推荐的吗?
下一篇:请教一家大家关于Sec+ 和 A+的就业问题
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表