532. K-diff Pairs in an Array

U盘文件变exe?3步教你用火绒+批处理脚本恢复隐藏文档(附注册表修复) 本文详细解析了U盘文件被病毒隐藏并伪装成exe文件的原理,并提供了三步解决方案:首先使用火绒安全软件进行全盘查杀清除病毒;其次通过批处理脚本批量恢复被隐藏的文件属性;最后修复被篡改的注册表设置,彻底解决文件无法显示的问题,帮助用户安全找回数据并加固系统防护。 阅读详情
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Example 1:
Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Note:
The pairs (i, j) and (j, i) count as the same pair.
The length of the array won't exceed 10,000.
All the integers in the given input belong to the range: [-1e7, 1e7].

这道题首先二话不说用O(

n2
)的解法秒掉,使用set:
class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        const int size = nums.size();
        if(size == 0 || k < 0)
            return 0;

        set<pair<int, int>> s;
        int a[2] = {0};
        for(int i=0; i<size-1; ++i){
            for(int j=i+1; j<size; ++j){
                if(nums[i] - nums[j] == k || nums[j] - nums[i] == k){
                    a[0] = nums[i];
                    a[1] = nums[j];
                    if(a[0] > a[1])
                        std::swap(a[0], a[1]);
                    s.insert(make_pair(a[0], a[1]));
                }
            }
        }
        return s.size();
    }
};

不过有更好的O(n)的解法,使用unordered_set,不过就是要注意k=0的情况要单独写出来,只有同一个数出现多次才被计算入结果。

深度学习工具包 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") 立即下载

相关推荐

Ubuntu20.04安装Torque-6.1.2单机版(踩坑篇)

说明:PBS排队系统原先是在Centos7安装使用很顺利,但是有客户需求在Ubuntu20.04上使用,着手在Ubuntu20.04安装上安装的时候发现很多报错,并不能顺利安装,网上查找到的教程,有的说是让放弃转slurm,有的是换源安装,但是安装好的版本很低,有许多bug,不是很好用。经过自己慢慢摸索之后,改了部分配置以及源码之后,能够顺利安装,再次记录一下踩坑心得,与大家一起分享学习。如有问题,欢迎大家来撩。...............

华青水上的博客 8107

K-diff Pairs in an Array

题面:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbe

chenjn69的博客 285

UE5新手必看:如何在UMG控件中嵌入浏览器实现Web页面展示(附完整步骤)

本文为UE5新手提供了在UMG控件中无缝集成Web浏览器的完整实战指南。详细介绍了从启用WebBrowser插件、创建并配置WebBrowser控件蓝图,到编写动态加载与交互逻辑,以及在游戏关卡中实例化UI的全过程。文章还分享了性能优化技巧与常见问题排查方法,帮助开发者高效实现游戏UI内的Web页面展示与交互。

weixin_29243063的博客 725

532. K-diff Pairs in an Array -- Python

532. K-diff Pairs in an Array Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), wh

Chris_zhangrx的博客 746

ADRV902x射频评估实战:手把手教你用TES GUI生成ARM bin与JESD204C配置文件

在射频前端系统设计中,ADRV902x系列收发器凭借其宽带性能和灵活配置能力,已成为5G基站、微波回传等场景的核心器件。然而当工程师面对非官方硬件平台时,如何高效利用Transceiver Evaluation Software(TES)完成关键配置,成为项目推进的第一道门槛。本文将彻底解析脱离ADS9母板后的完整离线工作流,从参数预设到文件生成,手把手解决真实工程中的配置痛点。在Demo模式下,所有硬件状态检测都会被跳过,但配置文件生成功能完全可用。作为基础模板,因其配置最为简洁,便于后续自定义修改。

weixin_33704591的博客 437

【亲测有效】OpenClaw Gateway 出现 “(no output)“ 卡死问题完整解决指南:从手动重启到 systemd Watchdog 自动恢复

最近在使用 OpenClaw(自托管 AI 助手框架,前身 Clawdbot/Moltbot)时,遇到了一个非常常见的顽疾:TUI(终端界面)或 Telegram 等通道经常显示 "(no output)",消息发出去后机器人完全没反应,看起来像是卡死了。

hampeter的博客 8634

leetcode题解-532. K-diff Pairs in an Array

题目:Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

呜呜哈的博客 3068

Ollama 在不同操作系统上的详细使用,及用python进行调用实战

本文介绍了Ollama的安装配置及Python调用方法。主要内容包括:1)Windows/macOS/Linux/Docker等多种安装方式;2)Ollama基础命令,涵盖模型管理、运行和服务控制;3)Python调用教程,包含官方库使用、自定义客户端、多轮对话、参数设置等高级功能,以及直接API调用示例。文章提供了完整的代码片段,适合开发者快速上手使用Ollama进行本地大模型部署和开发。

数据知道的博客 1243

532. K-diff Pairs in an Array*

532. K-diff Pairs in an Array* https://leetcode.com/problems/k-diff-pairs-in-an-array/ 题目描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array...

珍妮的选择的博客 294

基于AUTOSAR的汽车电子项目--电动助力转向系统

基于AUTOSAR的汽车电子项目开发通常涵盖广泛的领域,例如动力系统控制、车身电子、信息娱乐系统、高级驾驶辅助系统(ADAS)等。以下是一个简化的示例项目,展示了如何在AUTOSAR环境下开发一个电动助力转向系统(Electric Power Steering, EPS)的控制软件

MHD0815的博客 2144

手把手教你学simulink(31.31)--双向DCDC电源场景示例:设计并实现一个基于变频控制的双向降压变换

设计并实现一个基于 Simulink 的双向降压变换器,能够在充电模式(从高压侧向低压侧传输能量)和放电模式(从低压侧向高压侧传输能量)之间切换,确保输出电压稳定,并根据负载变化自动调整工作模式。

MHD0815的博客 349

leetcode解题之532. K-diff Pairs in an Array Java版

leetcode解题之532. K-diff Pairs in an Array Java版

mine_song的博客 1864

实用指南:制作通用的INCA和CANape兼容A2L文件

A2L(ASAM MCD-2 Measurement and Calibration Data)文件是描述ECU(电子控制单元)测量和校准数据的标准文件格式。在标定工作中,A2L文件扮演着极为重要的角色,它定义了测量通道、校准参数等关键信息,为工程师提供了统一的描述方式。INCA和CANape是两种常见的汽车电子系统标定工具,分别由ETAS和Vector公司开发。它们在汽车电子系统的开发和调试过程中发挥着重要作用,提供了丰富的标定功能和用户友好的界面。

一直在水些技术小文 2941

【Leetcode】532. K-diff Pairs in an Array

思路: 成对的值不分先后,所以先对nums进行排序。 用一个set存储出现过的值,用于后续判断是否某个值已经有值与其成对。 分为两种情况: (1)k==0,即找出值相等的对数。 再用一个sameSet存储所有已成对的值,避免同一个值加入结果多次。只有sameSet中不含该值,且set中包含了该值,才能加入结果。 (2)k!=0,即找出差的绝对值为k的对数。 只有set中不包含该值但包

筱葭的博客 4090

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【leetcode】532. K-diff Pairs in an Array

532. K-diff Pairs in an Arrayhttps://leetcode.com/problems/k-diff-pairs-in-an-array/#/description Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in th

xxxxzr的博客 883

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode 532 K-diff Pairs in an Array

LeetCode 532 K-diff Pairs in an Array题目思路代码 题目 Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array. A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true: 0 <= i, j < num

weixin_43796689的博客 136

Leetcode 532 K-diff Pairs in an Array

Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers

夜色之浓,莫过于黎明前的黑暗。 403

leetcode 532. K-diff Pairs in an Array

leetcode 532. K-diff Pairs in an Array    下面这种解法TLE  public class Solution { public int findPairs(int[] nums, int k) { if(nums==null||nums.length==0||k<0) return 0; int len = nums

独钓寒江雪 402

Leetcode——532. K-diff Pairs in an Array

题目原址 https://leetcode.com/problems/k-diff-pairs-in-an-array/description/ 题目描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a ...

想当厨子的程序媛 299

LeetCode //C - 532. K-diff Pairs in an Array

【代码】LeetCode //C - 532. K-diff Pairs in an Array。

Made in Code 903

LeetCode 532. K-diff Pairs in an Array

K-diff Pairs in an Array 题目描述: Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i

i逆天耗子 641
上一篇: 459. Repeated Substring Pattern
下一篇: 383. Ransom Note
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址