GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Create 923_3_sum_with_multiplicity.cpp (#30) · manondidi/leetcode@5dadae9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cpp
    • 923_3_sum_with_multiplicity.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cpp/923_3_sum_with_multiplicity.cpp‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int threeSumMulti(vector<int>& A, int target) {
4+
unordered_map<int, uint64_t> count;
5+
for (const auto& a : A) {
6+
++count[a];
7+
}
8+
uint64_t result = 0;
9+
for (const auto& kvp1 : count) {
10+
for (const auto& kvp2 : count) {
11+
int i = kvp1.first, j = kvp2.first, k = target - i - j;
12+
if (!count.count(k)) {
13+
continue;
14+
}
15+
if (i == j && j == k) {
16+
result += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
17+
} else if (i == j && j != k) {
18+
result += count[i] * (count[i] - 1) / 2 * count[k];
19+
} else if (i < j && j < k) {
20+
result += count[i] * count[j] * count[k];
21+
}
22+
}
23+
}
24+
return result % static_cast<int>(1e9 + 7);
25+
}
26+
};

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.