LeetCode 1588 Sum Of All Odd Length Subarrays Writeup
技術標籤:LeetCodePracticeleetcode資料結構演算法
所有奇數長度子陣列的和
from typing import List
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
sums = 0
for i in range(1,len(arr)+1,2): #步長從1到arr長度加一,左閉右開
j = 0
while i+j <= len(arr):
sums += sum(arr[j:j+i]) #逐個累加子陣列的和
j += 1
return sums
複雜度分析
- 時間複雜度:大概是O(n ^ 2),切片的複雜度不太清楚
- 空間複雜度:大概是O(n)
相關推薦
LeetCode 1588 Sum Of All Odd Length Subarrays Writeup
技術標籤:LeetCodePracticeleetcode資料結構演算法 所有奇數長度子陣列的和 原題 from typing import List
[LeetCode] 1588. Sum of All Odd Length Subarrays
Given an array of positive integersarr, calculate the sum of all possible odd-length subarrays. A subarray is a contiguoussubsequence of the array.
[LeetCode] 1781. Sum of Beauty of All Substrings
Thebeautyof a string is the difference in frequencies between the most frequent and least frequent characters.
LeetCode #985. Sum of Even Numbers After Queries
題目 985. Sum of Even Numbers After Queries 解題方法 遍歷陣列A求得所有偶數和evensum,然後遍歷queries陣列更新陣列A、偶數和evensum和返回值rat,這裡有一個巧妙的方法,可以先從evensum中減去當前即將被更改的
LeetCode:371. Sum of Two Integers Sum of Two Integers兩整數之和(C語言)
技術標籤:LeetCode 題目描述: 不使用運算子 + 和 - ,計算兩整數 a 、b 之和。
[LeetCode] 1748. Sum of Unique Elements
You are given an integer arraynums. The unique elements of an array are the elements that appearexactly oncein the array.
LeetCode 1524 Number of Sub-arrays With Odd Sum 思維
Given an array of integers arr, return the number of subarrays with an odd sum. Since the answer can be very large, return it modulo \\(10^9 + 7\\).
C. Given Length and Sum of Digits... (貪心)
https://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of th
Leetcode: 30. Substring with Concatenation of All Words
Description You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly
Leetcode: 327. Count of Range Sum
Descrption Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
【Leetcode】1685. Sum of Absolute Differences in a Sorted Array
技術標籤:# 棧、佇列、串及其他資料結構leetcode演算法 題目地址: https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/
【LeetCode】363. Max Sum of Rectangle No Larger Than K 矩形區域不超過 K 的最大數值和(Hard)(JAVA)
技術標籤:Leetcodeleetcode演算法java面試動態規劃 【LeetCode】363. Max Sum of Rectangle No Larger Than K 矩形區域不超過 K 的最大數值和(Hard)(JAVA)
[LeetCode] 1155. Number of Dice Rolls With Target Sum 擲骰子的N種方法
You haveddice and each die hasffaces numbered1, 2, ..., f. Return the number of possible ways (out offdtotal ways)modulo109+ 7 to roll the dice so the sum of the face-up numbers equalstarget.
[LeetCode] 1161. Maximum Level Sum of a Binary Tree 最大層內元素和
Given therootof a binary tree, the level of its root is1, the level of its children is2, and so on. Return thesmallestlevelxsuch that the sum of all the values of nodes at levelxismaximal.
Leetcode: 1508 Range Sum of Sorted Subarray Sums
Description You are given the array nums consisting of n positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, cre
【leetcode】圖演算法 834. Sum of Distances in Tree
There is an undirected connected tree withnnodes labeled from0ton - 1andn - 1edges.You are given the integernand the arrayedgeswhereedges[i] = [ai, bi]indicates that there is an edge between node
【leetcode】two-sum 變形 633. Sum of Square Numbers
Given a non-negative integerc, decide whether there\'re two integersaandbsuch thata2+ b2= c. 只要是two_sum 變形 都可以考慮用hash_set來做。
【leetcode】938. Range Sum of BST
Given therootnode of a binary search tree and two integerslowandhigh, returnthe sum of values of all nodes with a value in theinclusiverange[low, high].
[LeetCode] 1780. Check if Number is a Sum of Powers of Three
Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
CodeChef Sum of distances(分治)
CodeChef Sum of distances(分治) 題目大意 有一排點,每個點 i 向 \\(i + 1, i + 2, i + 3\\) 分別連價值為 \\(a_i,b_i,c_i\\) 的有向邊,問兩兩間最短路之和