头像

daijie24


访客:1963

离线:2天前



题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

class Solution(object):
    def findContinuousSequence(self, sum):
        """
        :type sum: int
        :rtype: List[List[int]]
        """
        res = []

        j = 1
        s = 1
        for i in range(1, sum + 1):

            while s < sum:
                j += 1
                s += j
            if s == sum and j - i > 0:
                temp = []
                for k in range(i, j + 1):
                    temp.append(k)
                res.append(temp)

            s -= i
        return res






题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

class Solution(object):
    def maxDiff(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0

        minv = nums[0]
        res = 0
        for i in range(len(nums)):
            minv = min(minv, nums[i])
            maxv = nums[i] - minv
            res = max(res, maxv)

        return res





题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

from collections import deque

class Solution(object):
    def maxInWindows(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        res = []
        q = deque([])

        for i in range(len(nums)):

            if len(q) != 0 and i - k >= q[0]:
                q.popleft()
            while len(q) != 0 and nums[i] > nums[q[-1]]:
                q.pop()

            q.append(i)

            if i >= k -1 :
                res.append(nums[q[0]])

        return res





daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

C++ 代码

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = float('-inf')

        s = 0
        for i in range(len(nums)):
            if s < 0:
                s = 0
            s += nums[i]
            res = max(res, float(s))
        return int(res)





daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python代码

class Solution(object):
    def getLeastNumbers_Solution(self, input, k):
        """
        :type input: list[int]
        :type k: int
        :rtype: list[int]
        """
        l = len(input)
        for i in range(k):
            for j in range(i + 1, l):
                if input[i] > input[j]:
                    input[i], input[j] = input[j], input[i]
        return input[0: k]



daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def merge(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        res = ListNode(-1)

        head = res


        while l1 and l2:
            if l1.val < l2.val:
                head.next = ListNode(l1.val)
                l1 = l1.next
            else:
                head.next = ListNode(l2.val)
                l2 = l2.next
            head = head.next
        if l1:
            head.next = l1
        if l2:
            head.next = l2

        return res.next






daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        pre = None

        cur = head

        while cur:
            next = cur.next
            cur.next = pre
            pre = cur
            cur = next
        return pre





daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def findKthToTail(self, pListHead, k):
        """
        :type pListHead: ListNode
        :type k: int
        :rtype: ListNode
        """
        l = 0
        head = pListHead
        while head:
            l += 1
            head = head.next
        if k > l:
            return None
        head = pListHead
        for _ in range(l - k):
            head = head.next
        return head




daijie24
12天前

题目描述

blablabla

样例

blablabla

算法1

(暴力枚举) $O(n^2)$

blablabla

时间复杂度

参考文献

python 代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplication(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None

        dummy = ListNode(-1)
        dummy.next = head

        p = dummy

        while p.next:
            q = p.next
            while q and q.val == p.next.val:
                q = q.next
            if q == p.next.next:
                p = p.next
            else:
                p.next = q
        return dummy.next



daijie24
5个月前

题目描述

将一个骰子投掷n次,获得的总点数为s,s的可能范围为n~6n。

掷出某一点数,可能有多种掷法,例如投掷2次,掷出3点,共有[1,2],[2,1]两种掷法。

请求出投掷n次,掷出n~6n点分别有多少种掷法。

样例

输入:n=2

输出:[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]

解释:投掷2次,可能出现的点数为2-12,共计11种。每种点数可能掷法数目分别为1,2,3,4,5,6,5,4,3,2,1。

      所以输出[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]。

算法1

(暴力枚举) $O(n^2)$

递归的方式

时间复杂度

参考文献

C++ 代码

class Solution {
public:
    vector<int> res;
    vector<int> numberOfDice(int n) {
        for(int i = n; i <= n * 6; i++) res.push_back(dfs(n, i));//列举出每种点数的投掷方法次数
        return res;
    }

    int dfs(int n, int sum){
        if(sum < 0) return 0;//表示该种方案不合适;
        if(n == 0) return !sum;//表示该种方案合适;
        int res = 0;
        for(int i = 1; i <= 6; i++){
            res += dfs(n-1, sum - i);//递归的方式,
        }
        return res;
    }
};


算法2

动态规划

将投掷方案按照最后一次的点数分类

时间复杂度

参考文献

C++ 代码

class Solution {
public:
    vector<int> numberOfDice(int n) {
        vector<vector<int>> f(n + 1, vector<int>(6 * n + 1, 0));//共投掷n次,和最大为6n
        f[0][0] = 1;
        vector<int> res;

        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= 6 * n; j++){
                for(int k = 1; k <= min(j, 6); k++){
                    f[i][j] += f[i-1][j - k];//f[i][j] 表示投掷i次,点数为j,k表示最后一次投掷的点数(热狗法)
                }
            }

        }
        for(int i = n; i <= 6 * n; i++){
            res.push_back(f[n][i]);
        }
        return res;
    }
};