cs: add «2657. Find the Prefix Common Array of Two Arrays»
Use ‹foreach› loop instead of a copy-pasta for checking both arrays at the same time. URL: https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f357df8fd6
commit
5af8d632b2
1 changed files with 6 additions and 10 deletions
|
@ -5,16 +5,12 @@ public class Solution {
|
|||
|
||||
var (common, result) = (0, new int[n]);
|
||||
for (var i = 0; i < n; ++i) {
|
||||
++freqs[xs[i]];
|
||||
if (freqs[xs[i]] == 2) {
|
||||
// occurred in both
|
||||
++common;
|
||||
}
|
||||
|
||||
++freqs[ys[i]];
|
||||
if (freqs[ys[i]] == 2) {
|
||||
// occurred in both
|
||||
++common;
|
||||
foreach (var x in new List<int>() { xs[i], ys[i] }) {
|
||||
++freqs[x];
|
||||
if (freqs[x] == 2) {
|
||||
// occurred in both
|
||||
++common;
|
||||
}
|
||||
}
|
||||
|
||||
result[i] = common;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue