LeetCode Single Number II

【AI基础学习系列】二十一、一文搞懂GPT 本文系统介绍了GPT(Generative Pre-trained Transformer)模型的核心内容。首先阐述了GPT的本质是基于Transformer的预训练语言模型,其创新在于使用单向Transformer编码器。其次详细解析了GPT架构,包括输入层的Tokenization和Embedding处理、Transformer解码器堆叠的核心组件(自注意力机制、前馈神经网络等)以及预训练和微调两阶段训练过程。接着说明了GPT的工作原理:通过无监督预训练学习语言统计规律,再结合有监督微调适应具体任务。 阅读详情

原题链接在这里:https://leetcode.com/problems/single-number-ii/

这道题的关键就是要求no extra memory space. 所以就用到了bit manipulation.

思路就是维护一个32位的数组,因为int 都是32位的,外层loop是对应每一位,内层loop是算所有的数在这一位上出现次数的和。

Mod 3 后剩下的就是那个只出现了一次的数。

Note: 1. Check if current bit is 1 using:

nums[j] >> i & 1
但要注意的是bit operator >> & 都比 == operator 运算级别低,所以要加括号。

2.  给了一个array代表2进制的数,转化成10进制的数就是用

res += bitCounter[i]<<i


AC Code:

public class Solution {
    public int singleNumber(int[] nums) {
        if(nums == null || nums.length == 0){
            return Integer.MIN_VALUE;
        }
        
        int res = 0;
        int [] bitCounter = new int[32];
        for(int i = 0; i < 32 ; i++){
            for(int j = 0 ; j < nums.length; j++){
                if((nums[j]>>i&1) == 1){ //error
                    bitCounter[i]= (bitCounter[i] + 1)%3;
                }
            }
        }
        for(int i = 0; i < 32; i++){
            res  += bitCounter[i]<<i;
        }
        
        return res;
    }
}


反爬虫:POST 请求参数中 Sign 的加密机制详解,并设计带 Sign 校验的网站 本文介绍了API请求中sign签名的生成原理及实现方法。sign是通过对请求参数加密生成的一串字符串,用于验证请求合法性,防止参数篡改、重放攻击和伪造请求。其生成步骤包括参数排序、拼接字符串、加盐和哈希加密。文章还演示了如何在FastAPI后端实现带sign验证的新闻网站,包括参数校验、时间戳验证和签名比对等安全措施,有效提升API安全性,增加爬虫逆向难度。 阅读详情

相关推荐

告别深度学习!用5个麦克风搞定多说话人分离:ReTM算法保姆级教程

本文详细介绍了ReTM算法,这是一种无需深度学习、仅需5个麦克风即可实现多说话人语音分离的创新信号处理方法。它通过估计相对传递矩阵,利用空间物理信息在混响环境中分离语音,具有数据需求低、计算复杂度小、无需先验几何信息等优势,特别适合嵌入式设备和资源受限场景。

desk3的博客 157

LeetCode解题报告 --- Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Single Number进阶,数组中只有一个数只出现一次,其他数都出现三次,找出单独的这个数。用线性时间复杂度和常数空间代价解题,并用位运算加速算法。

Sharon要学术 3414

对于基本的捕鱼游戏设计思路(五)——鱼

今天讲捕鱼游戏里鱼的部分,也是比较关键的一部分。 一步一步来,先分析游戏,首先鱼是从屏幕外生成,然后它有个移动的动作,而且分为不同的类型。 然后就可以进入代码部分,首先设置一个计时器,控制时间生成鱼,而鱼分种类,所以就用到随机函数,随机输出一个值,传入switch语句,从而生成不同的鱼。 void GameScene::xiaoyuupdate(float dt){ Size visibl

Cineze的博客 1万+

LeetCode_Single_Number_II

问题描述 给定一个整数数组,每个元素出现k (k > 1)次,只有一个元素出现p次(p >= 1, p % k != 0)。找到那个元素。 数组元素只有1位的特殊情况 正如其他人指出的,为了应用位操作,我们回顾一下整数在计算机中是如何表示的——按二进制位表示。首先,我们先来考虑只有1位的情况。假设我们有一个整数数组,它的元素只有1位二进制(0或者1),我们需要统计数组中1的数量,当1...

wlwh90的专栏 5867

virtualbox虚拟机里面最顶部没有控制、设置和帮助等选项

无意中操作,导致virtualbox虚拟机里面最顶部没有控制,设置和帮助选项  virtualbox虚拟机窗口显示如下如: 如何设置,窗口显示如下图所示呢: virtualbox虚拟机窗口模式显示设置,切换可以通过如下图所示操作进行切换: PS:一定要是右边的Ctrl+C

WEL测试 8780

即插即用系列(代码实践)| 2025 Strip R-CNN:大尺寸条形卷积——遥感目标检测新SOTA,专治细长目标(桥梁、船只)

摘要: 论文《Strip R-CNN: Large Strip Convolution for Remote Sensing Object Detection》针对遥感图像中细长目标(如桥梁、道路)检测难题,提出创新性解决方案。通过设计StripBlock模块,将传统方形卷积核替换为大尺寸条形卷积(水平1×19与垂直19×1组合),有效捕获长距离空间特征,同时减少背景噪声。结合CenterPooling增强目标中心定位,该模块可即插即用至现有网络,显著提升遥感检测任务(如DOTA数据集)的精度。代码已开源,

分享顶会顶刊论文即插即用模块 189

【Leetcode】Single Number、Single Number II、Single Number III

list操作 time:O(n2);space:O(n)/O(1)time:O(n^2);space:O(n)/O(1)time:O(n2);space:O(n)/O(1) 遍历list中的每一个元素,用额外空间记录遍历的数,这个额外空间tmp初始为空,遍历时,若存在,则从tmp中删除,不存在则加入tmp,由于只有一个数只出现了一次,其他数都是两次,所以最后tmp中只剩一个元素,将其输出。 ...

Ray 309

OpenStack详解、排错、经验总结

第1章 Openstack简介 1.1 计算(Compute):Nova。 一套控制器,用于为单个用户或使用群组管理虚拟机实例的整个生命周期,根据用户需求来提供虚拟服务。负责虚拟机创建、开机、关机、挂起、暂停、调整、迁移、重启、销毁等操作,配置CPU、内存等信息规格。自Austin版本集成到项目中。 1.2 对象存储(Object Storage):Swift。 一套用于在大规模可扩展系统中通过内置冗余及高容错机制实现对象存储的系统,允许进行存储或者检索文件。可为Glance提供镜像存储,为Cinder提供

weixin_40548182的博客 1974

[leetcode]137. Single Number II

[leetcode]137. Single Number II Analysis 唉 感觉应该GG了,还是好好刷题看论文吧—— [每天刷题并不难0.0] Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find...

weixin_32135877的博客 237

Python爬虫实战:逆向工程与异步处理攻克Pinterest无限滚动瀑布流

我们将从零开始,逐步分析其数据接口,构建一个功能完整的爬虫,并深入探讨反爬虫策略的应对、性能优化以及数据存储等高级主题。: 在实际爬取中,你需要先访问一次Pinterest主页,获取初始的Cookie和CSRF Token,然后再发起API请求。: Python爬虫,Pinterest,无限滚动,瀑布流,异步IO,httpx,逆向工程,API接口,动态内容,反爬虫。你会很快发现一些关键的请求模式。:页面初始加载有限内容,随着用户滚动,新的图片块通过AJAX动态加载,实现“永无止境”的浏览。

2201_76125261的博客 551

PHP实现获取当地网络时间​(附完整源码)

PHP实现获取当地网络时间​(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 236

[LeetCode] 137. Single Number II 深入浅出算法讲解和代码示例

137. Single Number II 深入浅出算法讲解和代码示例 本文采用了位运算的思路

karen0310的专栏 2638

手把手教你学GPU的KMD专栏--3.4 中断处理机制与优化

记住,中断处理是GPU与CPU通信的"桥梁",没有高效的中断处理,GPU的性能将无法充分发挥。KMD通过"快进快出"原则、事件标志机制、GPU本机围栏等技术,实现高效、低延迟的中断处理。:GPU本机围栏机制允许GPU在满足特定条件(如围栏值达到某个阈值)时才触发中断,而不是每次围栏更新都触发中断,从而显著减少CPU唤醒次数,降低功耗。正如Microsoft GPU架构师在2022年WinHEC大会上指出:"没有好的中断处理,再强大的GPU也会被系统延迟拖垮。:多个GPU设备共享中断,导致中断处理混乱。

MHD0815的博客 112

LeetCode 137. Single Number II(单个数字)

原题网址:https://leetcode.com/problems/single-number-ii/ Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a lin

James Pan 776

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

leetcode 137. Single Number II

https://leetcode.com/problems/single-number-ii/ 给定一个数组,该数组有一个元素只出现一次,其他元素均出现三次,找到这个只出现一次的元素,时间O(n),空间O(1) 一、问题分析 测试用例: Example: Input: [1,2,1,3,2,5] Output: [3,5] 设其他元素有k个,则所有元素每个位上0的数量=3*m...

小景的博客 272

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode刷题:137. Single Number II

LeetCode刷题:137. Single Number II 原题链接:https://leetcode.com/problems/single-number-ii/ Given a non-empty array of integers, every element appears three times except for one, which appears exactly onc...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 227

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode 137. Single Number II

题目来源:https://leetcode.com/problems/single-number-ii/ 问题描述 137.Single Number II Medium Given anon-emptyarray of integers, every element appearsthreetimes except for one, which appears exact...

da_kao_la的博客 282

LeetCode — Single Number II 三进制的使用

原文链接: LeetCode — Single Number II 三进制的使用 ...

阿豪 133

LeetCode Single Number I / II / III

【1】LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数。 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数。 代码如下: class Solution { public: int singleNumb...

weixin_30797027的博客 130

LeetCode之Single Number II

http://oj.leetcode.com/problems/single-number-ii/ Every element appears three times except for one,需要注意的特殊情况是 n == 1的情况 class Solution { public: int singleNumber(int A[], int n) {

叠搭宝箱 673

LeetCode | Single Number II

原题描述:https://oj.leetcode.com/problems/single-number-ii/

Young Z. Wang 983

LeetCode 137. Single Number II 解题报告

LeetCode 137. Single Number II 解题报告

骆小坑的填坑之旅 3089

LeetCode--136. Single Number & 137. Single Number II & 260. Single Number III

题目链接:https://leetcode.com/problems/single-number/、https://leetcode.com/problems/single-number-ii/、https://leetcode.com/problems/single-number-iii/ 思路一:最朴素的思路就是使用哈希算法来做这三条题目,代码都差不多 Single Number 代码: ...

To_be_thinking的博客 170

【异或】LeetCode 137. Single Number II

LeetCode 136. Single Number II Solution1:不会做,抄的 这道题是之前那道 Single Number 单独的数字 的延伸,那道题的解法就比较独特,是利用计算机按位储存数字的特性来做的,这道题就是除了一个单独的数字之外,数组中其他的数字都出现了三次,那么还是要利用位操作 Bit Operation 来解此题。我们可以建立一个32位的数字,来统计每一位上1出...

Allenlzcoder的博客 460

leetcode 137. Single Number II 四种解体思路!

题意 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。 说明: 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 示例 1: 输入: [2,2,3,2] 输出: 3 示例 2: 输入: [0,1,0,1,0,1,99] 输出: 99 解1 次数全部存下来,最简单,时间O(N),空间O(N) // Runtime: 3 ms, faster than 55.37% of Java online submissions for Single

1 259

【LeetCode】137. Single Number II 只出现一次的数字 II(Medium)(JAVA)

【LeetCode】137. Single Number II 只出现一次的数字 II(Medium)(JAVA) 题目地址: https://leetcode.com/problems/single-number-ii/ 题目描述: Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single o

qq_16927853的博客 183
上一篇: LeetCode 3Sum Closest
下一篇: LeetCode Single Number
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值