问答

当前位置

Question on %MOD

  • by Emma
  • 1
  • 1 月,1 周前
  • Ta: Anidlebrain

  1. Distinct Subsequences II
    Given a string S, count the number of distinct, non-empty subsequences of S .
    Since the result may be large, return the answer modulo 10^9 + 7.

我的代码是这样的,是过不了的,但是当我把所有的cnt/ps/inc/ret的type都改成long以后就ac了,不明白为什么,助教老师能帮忙解答下嘛,谢谢!

class Solution {
public int distinctSubseqII(String S) {
int MOD = 1_000_000_007;
int[] cnt = new int[26];
int ps = 0, inc = 0, ret = 0;
for (char c : S.toCharArray()) {
int org = cnt[c - 'a'];
cnt[c - 'a'] = (ps + 1);
ps = (ps - org + cnt[c - 'a']) % MOD;
}
for (int i = 0; i < 26; i++) {
ret += cnt[i];
ret %= MOD;
}
return ret;
}
}

1 个回复

2019-09-09 carry

是要用long的,因为数据可能会很大,超出int的范围,造成yi'chu

我来回答

您没有权限

为提高问答质量,问答版块发言权限只向九章学员开放

登录 注册

© Jiu Zhang 2013-. All rights reserved. 京ICP备16004690号-1