New Problem Solution - "1513. Number of Substrings With Only 1s"

This commit is contained in:
Hao Chen 2020-10-03 13:19:57 +08:00
parent 834c6837a5
commit 972bc2ea90
3 changed files with 124 additions and 0 deletions

View File

@ -10,10 +10,12 @@ LeetCode
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium|
|1556|[Thousand Separator](https://leetcode.com/problems/thousand-separator/) | [C++](./algorithms/cpp/thousandSeparator/ThousandSeparator.cpp)|Easy|
|1551|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/) | [C++](./algorithms/cpp/minimumOperationsToMakeArrayEqual/MinimumOperationsToMakeArrayEqual.cpp)|Medium|
|1550|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [C++](./algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp)|Easy|
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium|
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium|
|1513|[Number of Substrings With Only 1s](https://leetcode.com/problems/number-of-substrings-with-only-1s/) | [C++](./algorithms/cpp/numberOfSubstringsWithOnly1s/NumberOfSubstringsWithOnly1s.cpp)|Medium|
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/) | [C++](./algorithms/cpp/shuffleTheArray/ShuffleTheArray.cpp)|Easy|
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [C++](./algorithms/cpp/maximumProductOfTwoElementsInAnArray/MaximumProductOfTwoElementsInAnArray.cpp)|Easy|
|1460|[Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [C++](./algorithms/cpp/twoArraysEqualByReversingSubArrays/MakeTwoArraysEqualByReversingSubArrays.cpp)|Easy|

View File

@ -0,0 +1,70 @@
// Source : https://leetcode.com/problems/number-of-substrings-with-only-1s/
// Author : Hao Chen
// Date : 2020-10-03
/*****************************************************************************************************
*
* Given a binary string s (a string consisting only of '0' and '1's).
*
* Return the number of substrings with all characters 1's.
*
* Since the answer may be too large, return it modulo 10^9 + 7.
*
* Example 1:
*
* Input: s = "0110111"
* Output: 9
* Explanation: There are 9 substring in total with only 1's characters.
* "1" -> 5 times.
* "11" -> 3 times.
* "111" -> 1 time.
*
* Example 2:
*
* Input: s = "101"
* Output: 2
* Explanation: Substring "1" is shown 2 times in s.
*
* Example 3:
*
* Input: s = "111111"
* Output: 21
* Explanation: Each substring contains only 1's characters.
*
* Example 4:
*
* Input: s = "000"
* Output: 0
*
* Constraints:
*
* s[i] == '0' or s[i] == '1'
* 1 <= s.length <= 10^5
******************************************************************************************************/
class Solution {
public:
// 11 - 1+2
// 111 - 1+2+3
// 1111 - 1+2+3+4
// 11111 - 1+2+3+4+5
// so, we just simply find the length of continuous '1',
// then, the answer it len*(len+1)/2
int numSub(string s) {
long long len=0;
long long result=0;
for (auto c : s) {
if (c=='1') {
len++;
continue;
}
if (len > 0){
result += len*(len+1)/2;
len = 0;
}
}
result += len*(len+1)/2;
return result % 1000000007 ;
}
};

View File

@ -0,0 +1,52 @@
// Source : https://leetcode.com/problems/thousand-separator/
// Author : Hao Chen
// Date : 2020-10-03
/*****************************************************************************************************
*
* Given an integer n, add a dot (".") as the thousands separator and return it in string format.
*
* Example 1:
*
* Input: n = 987
* Output: "987"
*
* Example 2:
*
* Input: n = 1234
* Output: "1.234"
*
* Example 3:
*
* Input: n = 123456789
* Output: "123.456.789"
*
* Example 4:
*
* Input: n = 0
* Output: "0"
*
* Constraints:
*
* 0 <= n < 2^31
******************************************************************************************************/
class Solution {
public:
string thousandSeparator(int n) {
if (n==0) return "0";
int cnt=0;
string result;
while( n > 0 ){
int m = n % 10;
result.insert(result.begin(), ('0' + m) );
cnt++;
n /= 10;
if (cnt % 3 == 0 && n > 0) {
result = '.' + result;
}
}
return result;
}
};