刷题(一):各个刷题网站使用方法


1 Leetcode

1.1 样例

题目:

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

代码:

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        hashmap = {}
        for index, num in enumerate(nums):
            another_num = target - num
            if another_num in hashmap:
                return [hashmap[another_num], index]
            hashmap[num] = index
        return None

测试用例:

[2,7,11,15]
9

测试用例执行结果:

提交结果:

2 牛客网

2.1 样例

题目

字节跳动2018 万万没想到之聪明的编辑

我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:

1 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
2 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
3 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC

我特喵是个天才!我在蓝翔学过挖掘机和程序设计,按照这个原理写了一个自动校对器,工作效率从此起飞。用不了多久,我就会出任CEO,当上董事长,迎娶白富美,走上人生巅峰,想想都有点小激动呢!

……

万万没想到,我被开除了,临走时老板对我说: “做人做事要兢兢业业、勤勤恳恳、本本分分,人要是行,干一行行一行。一行行行行行;要是不行,干一行不行一行,一行不行行行不行。” 我现在整个人红红火火恍恍惚惚的……

请听题:请实现大锤的自动校对程序

代码:

def fun(s):
    if not s:
        return s
    else:
        tmp, res = '', ''
        for i in range(len(s)):
            if tmp == '':
                tmp += s[i]
            elif len(tmp) == 1:
                if s[i] != tmp[0]:
                    res += tmp
                    tmp = s[i]
                else:
                    tmp += s[i]
            elif len(tmp) == 2:
                if s[i] == tmp[0]:
                    continue
                else:
                    tmp += s[i]
            elif len(tmp) == 3:
                if s[i] != tmp[-1]:
                    res += tmp
                    tmp = s[i]
                else:
                    continue
        res += tmp
        return res
  
if __name__ == '__main__':
    for _ in range(int(input())):
        s = input().strip()
        res = fun(s)
        print(res)

测试用例:

输入

2
helloo
wooooooow

输出

hello
woow

测试用例执行结果:

提交结果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM