예제 #1
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(s):
    collect = [
        'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'
    ]
    num = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    ptr = 0
    ans = 0
    while len(s):
        pattern = collect[ptr]
        if s[:len(pattern)] == pattern:
            ans += num[ptr]
            s = s[len(pattern):]
        else:
            ptr += 1
    return ans


container(func, 'LVIII')
예제 #2
0
파일: 16.py 프로젝트: kawehburg/leetcode
                        return True
        return False

    if epoch(target):
        return target
    m = 0
    while True:
        if epoch(target + m):
            return target + m
        if epoch(target - m):
            return target - m
        m += 1


def ref(nums, target):
    import bisect
    nums, r, end = sorted(nums), float('inf'), len(nums) - 1
    for c in range(len(nums) - 2):
        i, j = max(
            c + 1,
            bisect.bisect_left(nums, target - nums[end] - nums[c], c + 1, end)
            - 1), end
        while r != target and i < j:
            s = nums[c] + nums[i] + nums[j]
            r, i, j = min(r, s, key=lambda x: abs(x - target)), i + (
                s < target), j - (s > target)
    return r


container(func, [-1, 2, 1, -4], 1)
예제 #3
0
파일: 22.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(n):
    ans = [('', n, 0)]
    for i in range(2 * n):
        collect = []
        for k in ans:
            if k[1] > 0:
                collect.append((k[0] + '(', k[1] - 1, k[2] + 1))
            if k[2] > 0:
                collect.append((k[0] + ')', k[1], k[2] - 1))
        ans = collect
    ans = [k[0] for k in ans]
    return ans


container(func, 5)




예제 #4
0
파일: 20.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(s):
    t = ['']
    close = {')': '(', '}': '{', ']': '['}
    for item in s:
        if item in close and t[-1] == close[item]:
            t.pop()
        elif item in close:
            return False
        else:
            t.append(item)
    if len(t) == 1:
        return True
    return False


def ref(s):
    while '{}' in s or '()' in s or '[]' in s:
        s = s.replace('{}', '')
        s = s.replace('[]', '')
        s = s.replace('()', '')
    return s == ''


container(func, '{[]}')
예제 #5
0
파일: 11.py 프로젝트: kawehburg/leetcode
    97, 22, 14, 59, 15, 96, 47, 1, 27, 65, 38, 69, 24, 58, 59, 45, 93, 37, 83,
    30, 55, 30, 37, 82, 3, 54, 8, 89, 67, 47, 74, 16, 69, 40, 27, 85, 36, 75,
    38, 64, 92, 29, 85, 68, 87, 96, 13, 32, 86, 96, 63, 41, 79, 52, 24, 82, 58,
    84, 71, 25, 32, 97, 41, 1, 89, 68, 38, 26, 95, 77, 90, 87, 6, 27, 56, 45,
    23, 69, 77, 9, 18, 92, 51, 97, 96, 27, 31, 54, 11, 54, 79, 95, 3, 72, 49,
    92, 93, 87, 70, 88, 16, 12, 28, 74, 39, 84, 19, 63, 5, 49, 72, 75, 93, 75,
    24, 90, 2, 55, 44, 66, 61, 76, 13, 64, 48, 62, 9, 93, 2, 79, 82, 70, 92,
    10, 45, 83, 46, 64, 46, 3, 65, 71, 79, 59, 98, 3, 49, 53, 59, 45, 19, 72,
    21, 84, 89, 22, 99, 98, 15, 1, 29, 49, 23, 73, 11, 68, 57, 57, 85, 55, 61,
    50, 78, 92, 9, 77, 95, 10, 30, 6, 56, 1, 79, 77, 85, 68, 99, 84, 18, 67,
    37, 47, 16, 61, 21, 28, 81, 30, 85, 66, 85, 98, 17, 64, 42, 26, 93, 38, 89,
    23, 96, 45, 24, 75, 74, 61, 95, 74, 46, 13, 93, 35, 13, 9, 96, 86, 37, 78,
    16, 75, 96, 53, 25, 13, 69, 68
]

container(func, sample)

# def func(height):
#     left = [0]
#     w = 0
#     for idx in range(1, len(height)):
#         h = height[idx]
#         tmp = 0
#         for i in range(len(left) - 1, -1, -1):
#             li = left[i]
#             this = (idx - li) * min(h, height[li])
#             if this < tmp:
#                 left.pop(i)
#             else:
#                 tmp = this
#                 w = max(w, this)
예제 #6
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(s):
    # cache记录每个字符在s中位置,center记录每个中心扩散出的回文串长度
    cache = defaultdict(list)
    center = defaultdict(int)
    for i, item in enumerate(s):
        for last in cache[item]:
            c = (last + i) / 2
            hl = i - last + 1
            if hl - center[c] <= 3:
                center[c] = hl
        cache[item].append(i)
    max_c = 0
    keys = list(center.keys())
    for c in keys:
        if center[c] > center[max_c]:
            max_c = c
    if center[max_c] == 0:
        center[max_c] = 1
    return s[int(max_c - center[max_c] / 2 + 0.5):int(max_c +
                                                      center[max_c] / 2 + 0.5)]


container(func, 'abc')
예제 #7
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(nums, target):
    left, right = 0, len(nums) - 1
    while left <= right:
        mid = (left + right) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] < nums[right]:
            if nums[mid] < target <= nums[right]:
                left = mid + 1
            else:
                right = mid - 1
        else:
            if nums[left] <= target < nums[mid]:
                right = mid - 1
            else:
                left = mid + 1
    return -1


container(func, [4, 5, 6, 7, 0, 1, 2], 3)
예제 #8
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(num):
    collect = [1000, 500, 100, 50, 10, 5, 1]
    minus = [100, 100, 10, 10, 1, 1, 0]
    collect_c = 'MDCLXVI'
    minis_c = 'CCXXII_'
    ptr = 0
    ans = ''
    while num > 0:
        if num >= collect[ptr]:
            ans += collect_c[ptr]
            num -= collect[ptr]
        elif num >= collect[ptr] - minus[ptr]:
            ans += minis_c[ptr] + collect_c[ptr]
            num -= collect[ptr] - minus[ptr]
        else:
            ptr += 1
    return ans


container(func, 1994)
예제 #9
0
파일: 27.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(nums, val):
    if len(nums) == 0:
        return 0
    j = 0
    for i in range(0, len(nums)):
        if nums[i] != val:
            nums[j] = nums[i]
            j += 1
    return j


container(func, [0, 1, 2, 2, 3, 0, 4, 2], 2)
예제 #10
0
파일: 6.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(s, numRows):
    if numRows <= 1:
        return s

    def row(x):
        tmp = x % (2 * numRows - 2)
        if tmp >= numRows:
            return 2 * numRows - 2 - tmp
        else:
            return tmp

    result = ['' for _ in range(numRows)]
    for i, item in enumerate(s):
        result[row(i)] += item
    return ''.join(result)


container(func, 'A', 5)
예제 #11
0
파일: 19.py 프로젝트: kawehburg/leetcode
def display_chain(head):
    ptr = head
    results = ''
    while ptr:
        results += str(ptr.val) + '->'
        ptr = ptr.next
    results = results[:-2]
    return results


def func(head, n):
    ptr = head
    i = 0
    delete = ptr
    di = 0
    while ptr:
        ptr = ptr.next
        if i - di == n + 1:
            delete = delete.next
            di += 1
        i += 1
    if i - di == n + 1:
        delete.next = delete.next.next
        return head
    else:
        return delete.next


container(func, build_chain([1, 2, 3, 4]), 3, display=display_chain)
예제 #12
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(digits):
    if len(digits) == 0:
        return []
    key = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
           '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
    ans = ['']
    for item in digits:
        collect = []
        for k in key[item]:
            collect.append([s + k for s in ans])
        ans = []
        for line in collect:
            ans.extend(line)
    return ans


container(func, '23')




예제 #13
0
파일: 9.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(x):
    x = str(x)
    reverse_x = x[::-1]
    return x == reverse_x


container(func, -121)
예제 #14
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(strs):
    if len(strs) == 0:
        return ''
    ans = strs[0]
    for s in strs:
        if len(s) == 0:
            return ''
        i = 0
        for i in range(min(len(ans), len(s))):
            if ans[i] != s[i]:
                break
        else:
            i += 1
        ans = ans[:i]
        if len(ans) == 0:
            return ''
    return ans


container(func, [["abab", "aba", ""]])
예제 #15
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(nums):
    n = len(nums)
    if n < 2:
        return nums
    i = n - 1
    while i > 0 and nums[
            i - 1] >= nums[i]:  # 要是前者大于等于后者 则不是要调整的目标 继续前移  !第一遍出错就是这儿没有等于
        i -= 1
    if i == 0 and nums[i] == max(nums):  # 此数为最大数
        return nums.reverse()
    else:  # 151    i=1
        j = n - 1
        while j > i - 1 and nums[j] <= nums[i - 1]:
            j -= 1
        temp = nums[i - 1]  # i-1为小数  j为大数 交换之
        nums[i - 1] = nums[j]
        nums[j] = temp
        re = nums[i:]
        for h in range(len(re)):
            nums[n - 1 - h] = re[h]
        return nums


container(func, [1, 3, 2])
예제 #16
0
                s.add(item)
            else:
                return True
        return False

    for i in range(9):
        if check(board[i]):
            return False
    for j in range(9):
        if check([board[i][j] for i in range(9)]):
            return False
    for k in [0, 3, 6]:
        for m in [0, 3, 6]:
            if check([
                    board[i][j] for i in range(k, k + 3)
                    for j in range(m, m + 3)
            ]):
                return False
    return True


container(func, [["5", "3", ".", ".", "7", ".", ".", ".", "."],
                 ["6", ".", ".", "1", "9", "5", ".", ".", "."],
                 [".", "9", "8", ".", ".", ".", ".", "6", "."],
                 ["8", ".", ".", ".", "6", ".", ".", ".", "3"],
                 ["4", ".", ".", "8", ".", "3", ".", ".", "1"],
                 ["7", ".", ".", ".", "2", ".", ".", ".", "6"],
                 [".", "6", ".", ".", ".", ".", "2", "8", "."],
                 [".", ".", ".", "4", "1", "9", ".", ".", "5"],
                 [".", ".", ".", ".", "8", ".", ".", "7", "9"]])
예제 #17
0
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(x):
    a = ''
    if x < 0:
        a = '-'
        x = -x
    x = str(x)
    x = a + x[::-1]
    x = int(x)
    if x > 2**31 - 1 or x < -2 ** 31:
        return 0
    return x

container(func, -120)




예제 #18
0
파일: 26.py 프로젝트: kawehburg/leetcode
from utils.utils import container, random_str
from collections import defaultdict
import random


def func(nums):
    if len(nums) == 0:
        return 0
    j = 1
    for i in range(1, len(nums)):
        if nums[i] != nums[j - 1]:
            nums[j] = nums[i]
            j += 1
    return j


container(func, [0])




예제 #19
0
def func(nums):
    if len(nums) < 3:
        return []
    collect = defaultdict(int)
    for item in nums:
        collect[item] += 1
    nums = sorted(list(set(nums)))
    ans = []
    for i in range(len(nums)):
        if collect[nums[i]] > 1:
            start = i
        else:
            start = i + 1
        for j in range(start, len(nums)):
            f = -nums[i] - nums[j]
            if nums[j] > f:
                break
            if f in collect:
                if f == nums[j] and collect[f] < 2:
                    pass
                elif f == nums[j] == nums[i] and collect[f] < 3:
                    pass
                else:
                    ans.append([nums[i], nums[j], f])

    return ans


container(func, [-1, 0, 1, 0])