楼主: ZYYYZ
跳转到指定楼层
上一主题 下一主题
收起左侧

Data Scientist Python Coding 题整理(持续更新

   
🔗
 楼主| ZYYYZ 2021-2-25 04:46:35 | 只看该作者
全局:
30. String mapping (leetcode 205)

Given two strings, str1 and str2, determine if there exists a one to one character mapping between each character of str1 to str2.

Example 1:

str1 = 'qwe'
str2 = 'asd'.1point3acres
. Χ
string_map(str1, str2) == True
#q = a, w = s, and e = d. 1point3acres
Example 2:

str1 = 'donut'
. 1point3acres.com str2 = 'fatty'

string_map(str1, str2) == False
#t cannot map to two different values

Method1:
  1. # T: O(n^2), S: O(1). 1point3acres.com
  2. class Solution:
  3.     def string_map_1(self, str1, str2):
  4.         if not str1 or not str2:
  5.             return True
  6.         
  7.         dic = {}
  8.         for i in range(len(str1)):
  9.             a = str1[i]
  10.             b = str2[i].
  11.             
  12.             if a in dic:
  13.                 if dic[a] == b:. 1point3acres
  14.                     continue
  15.                 else:
  16.                     return False
  17.                
  18.             else:
  19.                 if b not in dic.values():
  20.                     dic[a] = b
  21.                 else:
  22.                     return False ..
  23.                
  24.         return True
  25.    
  26.    
  27. if __name__ == '__main__':
  28.     assert Solution().string_map_1('qwe', 'asd') == True
  29.     assert Solution().string_map_1('donut', 'fatty') == False
  30.     assert Solution().string_map_1('egg', 'add') == True
复制代码



Method2:
  1. # T: O(n), S: O(1)
  2. class Solution:
  3.     def string_map_2(self, str1, str2):
  4.         if not str1 or not str2:. From 1point 3acres bbs
  5.             return True. .и
  6.         
  7.         str1_chars, str2_chars = [0]*256, [0]*256
  8.         
  9.         for i in range(len(str1)):
  10.             if str1_chars[ord(str1[i])] != str2_chars[ord(str2[i])]:
  11.                 return False. 1point 3 acres
  12.             else:.1point3acres
  13.                 str1_chars[ord(str1[i])] = str2_chars[ord(str2[i])] \
  14.                                          = ord(str2[i])
  15.                 . From 1point 3acres bbs
  16.         return True
  17.    
  18.    
  19. if __name__ == '__main__':
  20.     assert Solution().string_map_2('qwe', 'asd') == True
  21.     assert Solution().string_map_2('donut', 'fatty') == False
  22.     assert Solution().string_map_2('egg', 'add') == True
复制代码
.--

评分

参与人数 1大米 +1 收起 理由
czhmily + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| ZYYYZ 2021-2-25 18:49:18 | 只看该作者
全局:
本帖最后由 YZDH 于 2021-2-25 18:55 编辑 . 1point3acres.com

31. Merge k sorted arraysGiven k sorted arrays, create a combined list while maintaining sorted order.(这道题和leetcode 23其实是一道题,不过leetcod里面是list of linkedLists)
.google  и
Example:
Input: arrs = [[1, 4, 5], [1, 3, 4], [2, 6]]. Waral dи,
Output: [1, 1, 2, 3, 4, 4, 5, 6]

  1. # Use Python heapq module
  2. # T: O(nlogk)
  3. # S: O(n). 1point3acres

  4. import heapq

  5. class Solution:
  6.     def merge_k_arrays(self, arrs):. .и
  7.         if not arrs or len(arrs) == 0:
  8.             return []

  9.         result = []
  10.         min_heap = []

  11.         for arr in arrs:
  12.             i = 0
  13.             while i < len(arr):
  14.                 heapq.heappush(min_heap, arr[i]). 1point3acres.com
  15.                 i += 1

  16.         while min_heap:. From 1point 3acres bbs
  17.             result.append(heapq.heappop(min_heap))

  18.         return result
  19.    

  20. if __name__ == '__main__':. From 1point 3acres bbs
  21.     Solution().merge_k_arrays([[1, 4, 5], [1, 3, 4], [2, 6]]) == \
  22.                                [1, 1, 2, 3, 4, 4, 5, 6]
  23.     Solution().merge_k_arrays([[1, 3, 5, 7], [2, 4, 6, 8], [0, 9, 10, 11]]) == \. ----
  24.                                [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ].1point3acres
  25.     Solution().merge_k_arrays([]) == []
  26.     Solution().merge_k_arrays([[]]) == []
复制代码

. Χ
[/i]

评分

参与人数 1大米 +2 收起 理由
wujiayikelly + 2 又来更新啦,点赞!

查看全部评分

回复

使用道具 举报

🔗
遇见未见 2021-3-5 14:01:08 | 只看该作者
全局:
YZDH 发表于 2021-2-25 04:46
30. String mapping (leetcode 205)

Given two strings, str1 and str2, determine if there exists a o ...

关于这题是不是也可以直接检查str1 或者 str2有没有重复字符? 要想1:1 map的话任何一个都不能有重复的iterable
回复

使用道具 举报

🔗
xuchuhong3177 2021-3-11 15:57:43 | 只看该作者
本楼:
全局:
赞一个~~
回复

使用道具 举报

🔗
seamless 2021-3-15 04:49:26 | 只看该作者
本楼:
全局:
非常感谢
回复

使用道具 举报

全局:
这个太棒了!如果是leetcode题的话,请问楼主可以标上leetcode题号吗?我刚开始练python,在leetcode里搜不到题目,感谢
回复

使用道具 举报

全局:
收藏再三再四紫薯
回复

使用道具 举报

🔗
yswang9024 2021-6-18 11:46:10 | 只看该作者
全局:
感谢楼主分享 每天来瞄一眼
回复

使用道具 举报

全局:
ZYYYZ 发表于 2021-2-17 08:19. 1point3acres.com
29. Train Linear Regression and Logistic Regression by using gradient descent
参考
1. 斯坦福 CS229 ...

请问有没有用coefficient公式直接推导coefficient的版本呢?
回复

使用道具 举报

🔗
Danlina 2021-7-6 23:33:32 | 只看该作者
全局:
多谢楼主总结分享
回复

使用道具 举报

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

本版积分规则

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