6369. 左右元素和的差值
遍历n次,生成两个列表,right列表倒置,相减求绝对值
class Solution(object):
def leftRigthDifference(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
left=[]
right=[]
n=len(nums)
for i in range(n):
if(i==0):
lesum=0
resum=0
else:
lesum=lesum+nums[i-1]
resum=resum+nums[n-i]
left.append(lesum)
right.append(resum)
right.reverse()
# ans=numpy.array(left)-numpy.array(right)
print(left,right)
ans = [abs(left[j]-right[j]) for j in range(n)]
print(ans)
return ans
6368. 找出字符串的可整除数组
class Solution {
public:
vector<int> divisibilityArray(string word, int m) {
int n =word.size();
vector<int> ans(n);
long d=0;//int型下面乘10错误
for(int i=0;i<n;i++){
d=(d*10+word[i]-'0')%m;//char-'0'变为int
if(d==0) ans[i]=1;
}
return ans;
}
};
6367. 求出最多标记下标
class Solution {
public:
int maxNumOfMarkedIndices(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n=nums.size();
int left=0,right=(n>>1)+1,mid,i;
while(left+1<right){
mid=(left+right)>>1;
for(i=0;i<mid;i++)
{if(nums[i]<<1>nums[n+i-mid]) break;}
if (i==mid) left=mid;
else right=mid;
}return left<<1;
}
};