|
| 1 | +# [2575.找出字符串的可整除数组](https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/) |
| 2 | + |
| 3 | +<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> ,长度为 <code>n</code> ,由从 <code>0</code> 到 <code>9</code> 的数字组成。另给你一个正整数 <code>m</code> 。</p> |
| 4 | + |
| 5 | +<p><code>word</code> 的 <strong>可整除数组</strong> <code>div</code> 是一个长度为 <code>n</code> 的整数数组,并满足:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | +<li>如果 <code>word[0,...,i]</code> 所表示的 <strong>数值</strong> 能被 <code>m</code> 整除,<code>div[i] = 1</code></li> |
| 9 | +<li>否则,<code>div[i] = 0</code></li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>返回<em> </em><code>word</code> 的可整除数组。</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | + |
| 16 | +<p><strong>示例 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>输入:</strong>word = "998244353", m = 3 |
| 20 | +<strong>输出:</strong>[1,1,0,0,0,1,1,0,0] |
| 21 | +<strong>解释:</strong>仅有 4 个前缀可以被 3 整除:"9"、"99"、"998244" 和 "9982443" 。 |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>示例 2:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>输入:</strong>word = "1010", m = 10 |
| 28 | +<strong>输出:</strong>[0,1,0,1] |
| 29 | +<strong>解释:</strong>仅有 2 个前缀可以被 10 整除:"10" 和 "1010" 。 |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | + |
| 34 | +<p><strong>提示:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | +<li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 38 | +<li><code>word.length == n</code></li> |
| 39 | +<li><code>word</code> 由数字 <code>0</code> 到 <code>9</code> 组成</li> |
| 40 | +<li><code>1 <= m <= 10<sup>9</sup></code></li> |
| 41 | +</ul> |
| 42 | + |
| 43 | + |
| 44 | +<details> |
| 45 | +<summary>标签:</summary> |
| 46 | +['数组', '数学', '字符串'] |
| 47 | +</details> |
| 48 | + |
| 49 | +<details> |
| 50 | +<summary>难度:Medium</summary> |
| 51 | +喜欢:15 |
| 52 | +</details> |
| 53 | + |
| 54 | + |
| 55 | +---------- |
| 56 | + |
| 57 | +# 算法1 |
| 58 | + |
| 59 | +## 算法思路 |
| 60 | + |
| 61 | +blablabla |
| 62 | + |
| 63 | +## 复杂度分析 |
| 64 | + |
| 65 | +时间复杂度:$O(n)$ |
| 66 | + |
| 67 | +空间复杂度:$O(1)$ |
| 68 | + |
| 69 | +## 代码实现 |
| 70 | + |
| 71 | +```cpp [] |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +```java [] |
| 76 | +class Solution { |
| 77 | + public int[] divisibilityArray(String word, int m) { |
| 78 | + int n = word.length(); |
| 79 | + |
| 80 | + int [] res = new int[n]; |
| 81 | + long cursum = 0L; |
| 82 | + for (int i = 0; i < n; i ++){ |
| 83 | + char c = word.charAt(i); |
| 84 | + int x = (int)(c - '0'); |
| 85 | + cursum = cursum * 10 + x; |
| 86 | + cursum %= m; |
| 87 | + if (cursum % m == 0){ |
| 88 | + res[i] = 1; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return res; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +## 参考文献 |
| 99 | + |
0 commit comments