Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加0058区间和 JavaScript版本
  • Loading branch information
catherinexrk authored Sep 2, 2024
commit 0f5a20826a0a531669eb4819e2da67dac30d2f10
49 changes: 49 additions & 0 deletions problems/kamacoder/0058.区间和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,52 @@ if __name__ == "__main__":
main()

```

### JavaScript

``` JavaScript

function prefixSum() {
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let inputLines = [];
rl.on('line', (line) => {
inputLines.push(line.trim());
});

rl.on('close', () => {
// 读取项数 n
const n = parseInt(inputLines[0]);

// 使用前缀和,复杂度控制在 O(1)
let sum = new Array(n);
sum[0] = parseInt(inputLines[1]);

// 计算前缀和数组
for (let i = 1; i < n; i++) {
let value = parseInt(inputLines[i + 1]);
sum[i] = sum[i - 1] + value;
}

// 处理区间和查询
for (let i = n + 1; i < inputLines.length; i++) {
let [left, right] = inputLines[i].split(' ').map(Number);

if (left === 0) {
console.log(sum[right]);
} else {
console.log(sum[right] - sum[left - 1]);
}
}
});
}


```


AltStyle によって変換されたページ (->オリジナル) /