📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: tall_cool_13
跳转到指定楼层
上一主题 下一主题
收起左侧

开一贴刷题打卡

🔗
 楼主| tall_cool_13 2018-9-26 13:00:26 | 只看该作者
全局:
chargerhe 发表于 2018-9-26 04:12
我也在刷, 看这楼主给自己压力... 楼主很给力. 顺带问问 Pramp 使用体验怎么样

还行吧,不过我也才体验过两次呢。个人感觉题目太简单了点,两次一共遇到了三道题目,都是easy题,挑战性有点小
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-26 13:42:43 | 只看该作者
全局:
301 题只想到了最直接的方法,每个字符( or )只有两种可能 remove or keep, 直接递归尝试所有可能,我就知道肯定会超时
  1. class Solution(object):
  2.     def removeInvalidParentheses(self, s):
  3.         """
  4.         :type s: str
  5.         :rtype: List[str]
  6.         """
  7.         res = self.posCursion(0, s)
  8.         mmx_len = 0
  9.         for s in res:
  10.             mmx_len = mmx_len if mmx_len > len(s) else len(s)

  11.         return list(set([s for s in res if len(s) == mmx_len]))

  12.     def posCursion(self, startIndex, s):
  13.         if self.checkVaild(s):
  14.             return [s]

  15.         if s == "" or startIndex >= len(s):
  16.             return [""]

  17.         res = []
  18.         if s[startIndex] in ['(', ')']:
  19.             # remove it
  20.             res.extend(self.posCursion(startIndex, "%s%s" % (s[0:startIndex], s[startIndex + 1:])))
  21.             # keep it
  22.             res.extend(self.posCursion(startIndex + 1, s))
  23.         else:
  24.             res.extend(self.posCursion(startIndex + 1, s))
  25.         return res

  26.     def checkVaild(self, s):
  27.         sstack = []
  28.         for c in s:
  29.             if c == '(':
  30.                 sstack.append("(")
  31.             elif c == ")":
  32.                 if len(sstack) == 0 or sstack[-1] != "(":
  33.                     return False
  34.                 sstack.pop()
  35.             else:
  36.                 pass
  37.         return False if len(sstack) > 0 else True
复制代码
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-26 14:12:33 | 只看该作者
全局:
301的暴力破解法还有优化的方案

回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-26 14:13:21 | 只看该作者
全局:
tall_cool_13 发表于 2018-9-26 14:12
301的暴力破解法还有优化的方案

我傻了这个优化其实还是比较容易想到了
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-26 16:40:54 | 只看该作者
全局:
Leetcode 297,使用 val(left)(right)的模式
  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.left = None
  6. #         self.right = None

  7. class Codec:

  8.     def serialize(self, root):
  9.         """Encodes a tree to a single string.

  10.         :type root: TreeNode
  11.         :rtype: str
  12.         """
  13.         if root == None:
  14.             return "-"

  15.         return "%s(%s)(%s)" % (root.val, self.serialize(root.left), self.serialize(root.right))

  16.     def deserialize(self, data):
  17.         """Decodes your encoded data to tree.

  18.         :type data: str
  19.         :rtype: TreeNode
  20.         """
  21.         if data == "-":
  22.             return None

  23.         num = int(data[0:data.find("(")])
  24.         data = data[data.find("("):]
  25.         diff = 0
  26.         root = TreeNode(num)
  27.         for idx, c in enumerate(data):
  28.             if c == "(":
  29.                 diff += 1
  30.             elif c == ")":
  31.                 diff -= 1

  32.             if diff == 0:
  33.                 root.left = self.deserialize(data[1:idx])
  34.                 root.right = self.deserialize(data[idx + 2:len(data)-1])
  35.                 break

  36.         return root

  37. # Your Codec object will be instantiated and called as such:
  38. # codec = Codec()
  39. # codec.deserialize(codec.serialize(root))
复制代码
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-27 06:11:22 | 只看该作者
全局:
做了56题,排序一下就很容易知道答案怎么做了,不谈
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-27 09:48:19 | 只看该作者
全局:
Leetcode 76题,这个一道hard题目不过题目倒是挺简单,思路很清晰, 代码就不写了
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-27 09:51:25 | 只看该作者
全局:
从今天开始就准备开始做ra了,但愿ra的活不多可以轻松一点点,这样还有时间继续刷题
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-27 23:32:55 | 只看该作者
全局:
一个oa题目,给定一个整型数组,然后按二进制中的1的个数排序,如果1的个数相同按照数字本身排序,比较简单,几分钟搞定
  1. def func(nums):
  2.     if len(nums) == 0:
  3.         return []

  4.     res = []
  5.     for num in nums:
  6.         cnt = 0
  7.         temp = num
  8.         while temp:
  9.             cnt += 1
  10.             temp = temp & (temp - 1)
  11.         res.append((num, cnt))

  12.     res = sorted(res, key=lambda x: (-x[1], -x[0]))
  13.     return [v[0] for v in res]


  14. if __name__ == "__main__":
  15.     print func([2, 3, 49, 22, 22, 232, 56, 38])
复制代码
回复

使用道具 举报

🔗
 楼主| tall_cool_13 2018-9-27 23:43:54 | 只看该作者
全局:
tall_cool_13 发表于 2018-9-27 23:32
一个oa题目,给定一个整型数组,然后按二进制中的1的个数排序,如果1的个数相同按照数字本身排序,比较简单 ...

如果有负数的话不对会有死循环
回复

使用道具 举报

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

本版积分规则

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