2020-12-24 题目类型:简单
1
给你两个整数,n 和 start 。
数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。
请返回 nums 中所有元素按位异或(XOR)后得到的结果。
class Solution(object):
def xorOperation(self, n, start):
"""
:type n: int
:type start: int
:rtype: int
"""
nums = 0
for i in range(n):
nums ^= (start + 2*i)
return nums
2
给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。
class Solution(object):
def findNumbers(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0
for i in nums:
n = 1
while (i/10) > 0:
n += 1
i /= 10
if (n % 2) == 0:
ans += 1
return ans
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(1 for num in nums if len(str(num)) % 2 == 0)