C++用 -1 mask法達到branchless解Leetcode1513 Number of Substrings With Only 1s|Py3 1-liner
其實很容易,想練功,試試一行解,或是把if分岔拿掉
[C++無分岔解請進]
----
It's actually quite easy. If you want to practice, try a one-line solution, or remove if branches.
class Solution {
public:
const long long mod=1e9+7;
int numSub(string& s) {
long long ans=0, cnt=0;
for(char c: s){
cnt= -(c=='1') & (cnt+1);
ans+= -(c=='1') & cnt;
}
return ans%mod;
}
};
沒有留言:
張貼留言
HTML 編輯器