sums

LeetCode.303/304 Range Sum Query(2) - Immutable

梦想的初衷 提交于 2020-01-30 22:58:50
303.题目(求数组子序列元素之和) Given an integer array nums , find the sum of the elements between indices i and j ( i ≤ j ) , inclusive . Example : Given nums = [ - 2 , 0 , 3 , - 5 , 2 , - 1 ] sumRange ( 0 , 2 ) - > 1 sumRange ( 2 , 5 ) - > - 1 sumRange ( 0 , 5 ) - > - 3 Note : You may assume that the array does not change . There are many calls to sumRange function . 303.分析 class NumArray { // 思路:统计每个元素从index=0到当前元素的下表的元素之和 private int [ ] nums ; private int [ ] sums ; public NumArray ( int [ ] nums ) { this . nums = nums ; if ( nums != null && nums . length > 0 ) { sums = new int [ nums . length ] ;

d2crub学习2 算合计

好久不见. 提交于 2020-01-27 15:58:20
<template> <div> <d2-crud :columns="columns" :data="data" :options="options"/> </div> </template> <script> export default { data () { return { columns: [ { title: 'ID', key: 'id' }, { title: '姓名', key: 'name' }, { title: '数值 1', key: 'amount1' }, { title: '数值 2', key: 'amount2' }, { title: '数值 3', key: 'amount3' } ], data: [ { id: '12987122', name: '王小虎', amount1: '234', amount2: '3.2', amount3: 10 }, { id: '12987123', name: '王小虎', amount1: '165', amount2: '4.43', amount3: 12 }, { id: '12987124', name: '王小虎', amount1: '324', amount2: '1.9', amount3: 9 }, { id: '12987125', name: '王小虎', amount1:

LeetCode All in One 题目讲解汇总(持续更新中...)

半世苍凉 提交于 2019-12-23 22:26:17
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k , and we want to maximize the sum of all 3*k entries. Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one. Example: Input: [1,2,1,2,6,7,5,1], 2 Output: [0, 3, 5] Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5]. We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically

373. Find K Pairs with Smallest Sums**

寵の児 提交于 2019-12-22 01:51:20
373. Find K Pairs with Smallest Sums** https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/ 题目描述 You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k . Define a pair (u,v) which consists of one element from the first array and one element from the second array. Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums. Example 1: Input: nums1 = [ 1,7,11 ] , nums2 = [ 2,4,6 ] , k = 3 Output: [ [ 1,2 ] , [ 1,4 ] , [ 1,6 ] ] Explanation: The first 3 pairs are returned from the sequence: [ 1,2 ] , [ 1,4 ] , [ 1,6 ] , [ 7,2 ] , [

【CODE】Continuous Subarray Sum

隐身守侯 提交于 2019-12-12 01:29:34
523. Continuous Subarray Sum Medium 8931240FavoriteShare Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k , that is, sums up to n*k where n is also an integer . Example 1: Input: [23, 2, 4, 6, 7], k=6 Output: True Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6. Example 2: Input: [23, 2, 6, 4, 7], k=6 Output: True Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42. Note: The length of the array

leetcode1281

北城余情 提交于 2019-12-08 11:46:27
1 class Solution: 2 def subtractProductAndSum(self, n: int) -> int: 3 s = str(n) 4 mul,sums = 1, 0 5 for i in range(len(s)): 6 cur = int(s[i]) 7 mul *= cur 8 sums += cur 9 return mul - sums 一次循环,同时记录乘积与加和。循环结束,两值再相减。 来源: https://www.cnblogs.com/asenyang/p/12005104.html

[Dynamic Programming] 198. House Robber

放肆的年华 提交于 2019-12-06 08:21:32
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. Input: [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3

Python: Partial sum of numbers [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: How to find the cumulative sum of numbers in a list? 15 answers can you help me with code which returns partial sum of numbers in text file? I must import text file, then make a code for partial sums without tools ..etc. My input: 4 13 23 21 11 The output should be (without brackets or commas): 4 17 40 61 72 I was trying to make code in python, but could only do total sum and not partial one. If i use the += operator for generator, it gives me an error! 回答1: Well, since everyone seems to be giving

Number of distinct sums from non-empty groupings of (possibly very large) lists

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Assume that you are given a set of coin types (maximum 20 distinct types) and from each type you have maximum 10^5 instances, such that the total number of all the coins in your list is maximum 10^6. What is the number of distinct sums you can make from non-empty groupings of these coins. for example, you are given the following lists: coins=[10, 50, 100] quantity=[1, 2, 1] which means you have 1 coin of 10, and 2 coins of 50, and 1 coin of 100. Now the output should be possibleSums(coins, quantity) = 9. Here are all the possible sums: 50 =

Any way for matlab to sum an array according to specified bins NOT by for iteration? Best if there is buildin function for this

匿名 (未验证) 提交于 2019-12-03 00:53:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: For example, if A = [7,8,1,1,2,2,2]; % the bins (or subscripts) B = [2,1,1,1,1,1,2]; % the array then the desired function "binsum" has two outputs, one is the bins, and the other is the sum. It is just adding values in B according to subscripts in A. For example, for 2, the sum is 1 + 1 + 2 = 4, for 1 it is 1 + 1 = 2. [bins, sums] = binsum(A,B); bins = [1,2,7,8] sums = [2,4,2,1] The elements in "bins" need not be ordered but must correspond to elements in "sums". This can surely be done by "for" iterations, but "for" iteration is not