当前位置:网站首页>只出现一次的数字 II(哈希、位操作、逻辑电路、有限状态自动机)
只出现一次的数字 II(哈希、位操作、逻辑电路、有限状态自动机)
2022-04-21 12:17:00 【4nc414g0n】
只出现一次的数字 II
题目链接
题目描述:
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素
方法1:哈希
思路:
- 建立一个数字用来映射次数
- 遍历拿出次数次数为1的
方法2:位操作
思路:
- 以[2,2,3,2]为例:
- 由于其他数都是3个,把每个数的每个位二进制的之和填入32个元素的数组
- 遍历%3,得到的就是那个数
代码如下:class Solution { public: int singleNumber(vector<int>& nums) { vector<int> bit_arr(32,0); for(int i=0;i<nums.size();i++) { int bit=32; while(bit--) { bit_arr[bit]+= (1&(nums[i]>>bit)); } } int ret=0; for(int i=0;i<32;i++) { if(bit_arr[i]%3==1) { ret|=(1<<i);//例如左移3位 0000001000,按位或达到效果 } } return ret; } };
方法3:逻辑电路
参考 :官方解题思路
方法4:有限状态自动机
参考: Krahets的解题思路
版权声明
本文为[4nc414g0n]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41420788/article/details/124197119
边栏推荐
- vscode 经常弹出:尝试在目标目录创建文件时发生一个错误 重试 跳过这个文件 关闭安装程序
- 最佳实践 | 通过使用 Jira Service Management 改进 HR 工作流程
- 国内期货开户去哪里开可以费率低,公司又安全?
- 机器学习-Sklearn-12(回归类大家族-上——多元线性回归、岭回归、Lasso)(解决多重共线性)
- 【软件测试系列五】《软件测试申请表》
- 俄外交部官员:在乌特别军事行动将在北约造成的安全威胁被消除后结束
- Extract name from given string (string, list, re "zero width assertion")
- Machine learning - sklearn-12 (regression family - upper - multiple linear regression, ridge regression, Lasso) (solve multiple collinearity)
- Machine learning-sklearn-13 (regression family - lower - nonlinear problem: polynomial regression (a new characteristic matrix is formed after polynomial transformation))
- 京东云分布式数据库StarDB顺利完成多方国产化适配认证
猜你喜欢

上海疫情中,新消费品牌们的坚守、放弃与救赎

Anchor free new network: centernet and cornernet form centernet + +

笑死!程序员延寿指南开源了
![[dark horse morning post] Tao Hong paid 260 million dividends from Zhangting MLM company in three years; Yiling pharmaceutical replied to Lianhua Qingwen dispute: the case has been reported; Tencent v](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Tao Hong paid 260 million dividends from Zhangting MLM company in three years; Yiling pharmaceutical replied to Lianhua Qingwen dispute: the case has been reported; Tencent v

Machine learning-sklearn-13 (regression family - lower - nonlinear problem: polynomial regression (a new characteristic matrix is formed after polynomial transformation))

Redis数据库集群(主从复制、哨兵、Cluster)

CVPR 2022 oral | Hong Kong Chinese open source posec3d: skeleton motion recognition framework based on 3d-cnn

Live broadcast of changes in the Jianghu: Li Jiaqi is trapped in a luxury house, Luo Yonghao fades out, and Weiya Sydney helps broadcast it

给定字符串提取姓名(字符串、list、re“零宽断言”)

数据库,把另一张表某字段数据添加到另一个表中,同时加入的时候加入自己需要的数据
随机推荐
Study notes of deep learning (6)
What's the difference between you and the architect? How to improve
Kubernetes 服务发现 监控APIServer
Vscode often pops up: an error occurred when trying to create a file in the target directory. Try again. Skip this file and close setup
[software test series vi] software system test scheme
上海疫情中,新消费品牌们的坚守、放弃与救赎
S参数简介
S parameter introduction
Seven ways of text reading based on C
《深度学习》学习笔记(五)
华润怡宝被传IPO,农夫山泉或将迎来早年“宿敌”
Reverse crawler 30 verification code of a fourth generation slider
逆向爬虫30 某验四代滑块验证码
Cultdao, a decentralized VC platform, launched a vote on the betamars project, which is currently in progress
【youcans 的 OpenCV 例程200篇】159. 图像处理之固定阈值法
The persistence, abandonment and redemption of new consumer brands in the Shanghai epidemic
Sublime theme colors
The non display of markerarray caused by the ROS version problem has been solved
总有一天,阿里系数据库会将Oracle挤出市场
Extract name from given string (string, list, re "zero width assertion")
