Đề bài
Build a running sum: the value at position i is the sum of all numbers from position 0 through i.
Implement running_sum(numbers).
Input
numbers: The list of integers.
Output
Return the list of running totals.
Ràng buộc
0 ≤ length ≤ 100,000; totals fit in a 64-bit integer
Ví dụ
Đầu vào:
numbers = [1, 2, 3, 4]
Kết quả mong đợi:
expected = [1, 3, 6, 10]
Đầu vào:
std::vector<long long> numbers = {1, 2, 3, 4};
Kết quả mong đợi:
std::vector<long long> expected = {1, 3, 6, 10};
Đầu vào:
long[] numbers = new long[] {1, 2, 3, 4};
Kết quả mong đợi:
long[] expected = new long[] {1, 3, 6, 10};
Giải thích
Totals so far: 1, 1+2=3, 3+3=6, 6+4=10.
Đầu vào:
numbers = [5, -5, 5]
Kết quả mong đợi:
expected = [5, 0, 5]
Đầu vào:
std::vector<long long> numbers = {5, -5, 5};
Kết quả mong đợi:
std::vector<long long> expected = {5, 0, 5};
Đầu vào:
long[] numbers = new long[] {5, -5, 5};
Kết quả mong đợi:
long[] expected = new long[] {5, 0, 5};
Giải thích
Totals: 5, 0, 5.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Each output element depends on the total of all earlier elements and itself.
Gợi ý 2
Keep a cumulative total as you move through the list.
Gợi ý 3
The first position should simply reflect the first input value.
Độ phức tạp mong đợi
O(n) time, O(n) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.