力扣刷题 day55:10-25

1.数组异或操作

给你两个整数,n 和 start 。

数组 nums 定义为:numsi = start + 2*i(下标从 0 开始)且 n == nums.length 。

请返回 nums 中所有元素按位异或(XOR)后得到的结果。

方法一:位运算

python 复制代码
#方法一:位运算
def xorOperation(n,start):
    res=0
    for i in range(n):
        res^=(start+2*i)  #逐个异或
    return res

2.统计一致字符串的数目

给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。

请你返回 words 数组中 一致字符串 的数目。

方法一:位运算

python 复制代码
#方法一:位运算
def countConsistentStrings(allowed,words):
    target=0
    for a in allowed:
        target|=1<<ord(a)-ord('a')  #用三十二位数存储出现的字母例如ab 11 abd 1011
    res=0
    for w in words:
        tem=0
        for i in w:
            tem|=1<<ord(i)-ord('a') #左移 如1<<2 100
        res+=(target|tem) ==target #看看或后等不等于目标值,如1011 | 1010==1011
    return res
相关推荐
Frostnova丶4 小时前
【算法笔记】数学知识
笔记·算法
吴可可1234 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白6 小时前
哈希:以时间换空间的算法实战
算法
啦啦啦啦啦zzzz7 小时前
数据结构:红黑树理论
数据结构·c++·红黑树
San813_LDD7 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859577 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta19988 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油9 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero9 小时前
Guava Cache Deep Dive
java·后端·算法·guava