Problem: 2575. 找出字符串的可整除数组
文章目录
- 思路
- 复杂度
- Code
思路
👨🏫 灵神题解
复杂度
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
Code
class Solution {
public int[] divisibilityArray(String word, int m)
{
int n = word.length();
long x = 0;
int[] ans = new int[n];
char[] words = word.toCharArray();
for (int i = 0; i < n; i++)
{
long y = words[i] - '0';
if ((x* 10 + y) % m == 0)
{
ans[i] = 1;
x = 0;
} else
{
ans[i] = 0;
x = (x * 10 + y) % m;
}
}
return ans;
}
}