JAVA去掉一个已排序数组的重复数字

82.给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。实现返回已排序的链表 通过对类似问题的分析和归纳,可以发现一些通用的解决方案思路,这些思路可以帮助我们更高效地解决链表相关的问题。在实际应用中,需要根据具体问题的要求进行适当的调整和优化。给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字。返回 已排序的链表。输入:head = [1,2,3,3,4,4,5]输入:head = [1,1,1,2,3]链表中节点数目在范围 [0, 300] 内。题目数据保证链表已经按升序 排列。输出:[1,2,5] 阅读详情
原文地址:http://www.java2000.net/p11764

论坛讨论地址: http://topic.csdn.net/u/20081109/14/EB2F41BF-52C3-4F9A-A7AD-F590A83887CC.html

我的一个方案,不过肯定有提升速度的地方
  1. import java.util.Arrays;

  2. /**
  3.  * JAVA去掉一个已经排好序的数组的重复数字,尽量快.
  4.  * 
  5.  * @author 老紫竹 JAVA世纪网(java2000.net)
  6.  * 
  7.  */
  8. public class Test {

  9.   public static void main(String args[]) {
  10.     int[] arr = { 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11,
  11.         11, 12, 12, 13, 14, 14, 15 }; // 预设数据数组
  12.     int index = 1; // 保存最后一个不重复的位置
  13.     int last = arr[0];
  14.     for (int i = 1; i < arr.length; i++) {
  15.       if (arr[i] != last) {
  16.         arr[index] = arr[i];
  17.         last = arr[index];
  18.         index++;
  19.       }
  20.     }
  21.     int[] rtn = new int[index];
  22.     System.arraycopy(arr, 0, rtn, 0, index);
  23.     System.out.println(Arrays.toString(rtn));
  24.   }
  25. }

特别是步长的部分。

LC整流滤波电路DIY:电感电容配合,手把手搭建低纹波直流电源 在直流电源设计中,整流滤波是连接交流输入与稳定输出的关键环节。常见的电容滤波虽然简单,但在大负载下纹波抑制能力有限,电感抵抗电流突变、电容抵抗电压突变,二者组合构成的LC滤波能有效降低输出电压纹波,同时减小开机浪涌电流。在工频线性电源、功放电源等对噪声敏感的场景中,LC滤波常作为稳压前的平滑缓冲。本文以一套完整DIY项目为线索,介绍整流桥后串联功率电感、并联滤波电容的电路构成,结合实际计算与波形测试,讨论电感量估算、电容选取、谐振风险、饱和电流等工程问题,并给出上电测试和故障排查方法。通过搭建与调试,可直观 阅读详情

相关推荐

2025年从零开始:构建你的首个ComfyUI工作流-ComfyUI入门教程

然而,由于它拥有众多的插件节点,以及较为复杂的操作流程,学习起来相对困难。以前面搭建的文生图工作流为例,首先点击功能区的【保存】,然后你的浏览器会出来一个弹窗,你可以进行文件命名,点击确定后浏览器会下载该文件,也就是带“json”后缀的文件,这样我们的工作流就打包成一份文件了。思考一下前面说的,节点是不是需要下载安装才能使用,而我们复用别人的工作流,他用的节点你不一定下载了,因为目前有600多个节点,你不可能全部下载了吧,所以我们日后使用别人的工作流,你会经常出现这样的问题。

weixin_59486588的博客 1466

数组的三种定义方式

数组的三种定义方式 1.数组概念 数组概念: 数组就是存储数据的容器,保证多个数据的数据类型要一致,长度固定。 2.数组的定义方式一 格式: 数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度] 数组定义格式详解: 数组存储的数据类型: 创建的数组容器可以存储什么数据类型。 [] : 表示数组。 数组名字:为定义的数组起个名,满足标识符规范...

无关痛痒 7万+

亿图图示(edrawmax中文版) v10.1.7pjb

edrawmax中文名叫做亿图图示,是一款非常棒的综合图形图表设计软件,既能实现流程图、架构图、工程图、思维导图等数百种专业领域图形图表的绘制,又能提供一个白板进行头脑风暴和任意编排,轻松实现数据和创意的可视化呈现。软件支持Windows、Mac、Linux及网络版,一端创作,多端同步,从而为跨端办公绘图提供新的解决方案,还兼容常用文档类的办公软件格式,包括图片、Office、PDF、Visio、Html、SVG等,尽可能的在文件格式这块满足用户多方面需求。软件支持创建260多种图表类型,拥有超过12500

qq_43293214的博客 1万+

数组的定义

数组

m0_74440974的博客 9600

MPU6050 vs BMX055 vs ICM20602:如何为你的项目选择最佳陀螺仪(附实测数据对比)

本文深度对比了MPU6050、BMX055和ICM20602三款主流六轴IMU传感器的核心性能。通过实测数据,分析了它们在精度、噪声、温漂、接口速度及资源占用上的差异,并提供了从项目需求出发的选型指南,帮助开发者根据预算、精度要求和处理器性能选择最合适的陀螺仪与加速度计组合。

h0i1j2k3l的博客 976

RV1126B芯片实战:如何用这款视觉处理器SoC打造你的第一个AI摄像头项目

本文详细介绍了如何利用瑞芯微RV1126B视觉处理器SoC从零构建AI摄像头项目。涵盖硬件选型、开发环境搭建、ISP配置、AI模型部署及系统集成等关键步骤,帮助开发者高效实现边缘计算方案,特别适合智能安防和工业检测应用。

dropout6artist的博客 943

数组的定义及使用

一、基本概念 数组指的就是一组相关变量的集合。例如:如果现在要想定义100个整型变量,如果按照传统的思路: int i1,i2,……i100,一共写100个变量。 这100个变量满足逻辑要求,但是没有任何的逻辑的控制关系,完全独立,那么就会出现对象不方便管理的情况。这种情况下,就可以利用数组来解决此类问题,而数组本身也属于引用数据类型。数组的定义语法如下: ·  声明并开辟数组:数据类型...

L. 2万+

计算机组成原理实验logisim(四)之运算器实验

注意,上半张图中所有连入32位宽多路选择器的器件和的“数据位宽”都是32,而中间部分连入1位多路选择器的器件的“数据位宽”皆为1(除了连向“32位加法器”的那一个非门,是32位)。第二到第五个实验的测试,具体怎么测试好吧我也不太清楚,听室友说就是让你分析各种亮灯的原理,比如你点这个,那个为什么颜色要变等等,我就不粘图了,恩大家加油。连图如下,需要在连线之前格外注意“异或门”和“与门”的位宽,将16改为1,不然数据无法通行。关于学术问题,为何这样连什么的,就别问我了,我也没有搞的那么透彻。

qq_54422132的博客 2万+

c语言数组定义详解

数据类型 + 数组名字 [数组大小,也就是长度] = { 数组的内容,用逗号隔开};在C语言中,数组可以储存同一种数据类型的数据,比如储存数字、字符、字符串等等。而arr[i]表示的就是i索引具体的数组的某一个值。再次强调:数组中括号内的数字,代表的是数组的索引!该数组,长度为5,但是索引是从0开始的,

qq_51216031的博客 1万+

kettle实验十一:没有数据流入时停止操作

【实验目的】 1.利用Kettle的“阻塞数据直到步骤都完成”,“检测空流”,“中止”等组件,实现Kettle转换流程的控制。 2.熟练掌握“阻塞数据直到步骤都完成”,“检测空流”,“中止”等组件的使用,实现Kettle转换流程的控制。 【实验原理】 “检测空流”组件可以检测所在转换的输入数据,如果整个转换没有输入数据,通过“中止”组件终止整个转换的流程。在存在数据输入的情况,为了能让数据输出,需要和“阻塞数据直到步骤都完成”配合使用。当所有数据都已经从上个步骤“字段选择”读取完毕后,通过“阻塞数据直到步骤

weixin_44727274的博客 3855

SillyTavern本地AI聊天平台部署与调优全指南

大型语言模型(LLM)作为当前人工智能领域的核心技术,通过其强大的自然语言理解和生成能力,正在深刻改变人机交互方式。其工作原理基于Transformer架构,通过海量数据训练,学习语言的统计规律与语义关联。这项技术的核心价值在于,它能够作为通用基础,赋能于多样化的下游应用,实现个性化的智能对话与内容创作。在工程实践中,如何高效、灵活且低成本地部署和利用这些模型,成为开发者与爱好者的关键需求。本地化部署方案,特别是结合如Ollama、KoboldCPP等推理引擎,提供了数据隐私保障与可控的硬件成本。Silly

weixin_42520448的博客 440

C语言基础入门----(5)数组详解

前面几篇文章我们有讲解过C中的常见数据类型,而本篇我们将讲解一种我们人为构造的数据类型——数组。同时,数组也是C中第一个开始我们重点掌握的内容,熟悉了数组,我们才能做出更多的操作。

cai_bi_A_LV的博客 1万+

【单片机】STM32F103C8T6 最小系统板原理图

STM32F103C8T6是一款基于ARM Cortex-M3内核的32位微控制器,由STMicroelectronics(ST)公司生产。它是STMicroelectronics的STM32系列微控制器中的一员,被广泛应用于嵌入式系统和电子设备中。LQFP48封装(Low-profile Quad Flat Package,48引脚)SRAM(静态随机访问存储器):20 KB。支持JTAG调试和编程接口(可选)多个通用输入/输出口(GPIO)USB接口(用于USB设备模式)内置调试和编程接口(SWD)

q742971636的博客 6万+

java计蒜客试题排序后的数组删除重复数字

import java.util.Scanner; public class Main {     public static void main(String[] args) {         // TODO Auto-generated method stub      Scanner sc=new Scanner(System.in);      int  n=sc.ne

pioneer的博客 261

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

JAVA去掉数组重复数字

去掉一个已经排好序的数组的重复数字,执行速度也是考虑的问题。 package JAVA_Project_01_04;//创建一个包 /* 提供两种思路解决去掉重复数字:第一种是增加一个数组,但是长度无法确定,记录没有重复的值; 第二种是增加一个数组,用于记录原数组中存在的数,再增加一个数组可以确定数组的长度,用于存放原数组的值。 */ public class TextDelRepeat {//...

江南一只猫 1893

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Java给定一个已排序的数组,使用就地算法将重复的数字移除,使数组中的每个元素只出现一次,返回新数组的长度。

题目描述 给定一个已排序的数组,使用就地算法将重复的数字移除,使数组中的每个元素只出现一次,返回新数组的长度。 不能为数组分配额外的空间,你必须使用常熟级空间复杂度的就地算法。 例如, 给定输入数组A=[1,1,2], 你给出的函数应该返回length=2,A数组现在是[1,2]。 public int removeDuplicates(int[] A) { if(A == null || A.length == 0){ return 0; ..

luzhensmart的专栏 1227

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

JAVA去掉一个已经排好序的数组的重复数字,尽量快

import java.util.Arrays; /** * JAVA去掉一个已经排好序的数组的重复数字,尽量快. * * @author 老紫竹 JAVA世纪网(java2000.net) * */ public class Test { public static void main(String args[]) { int[] arr = { 1, 2, 2, ...

通吃互联网 215

java找出不重复的数_关于Java:在整数数组中查找第一个非重复数

我有一个考试题:Given an integer array find the first number which is not repeating in array using O(N) time complexity and O(1) space complexity.我想不出任何解决方案。 我知道我可以遍历数组并维护一个linkedhashmap,它将存储数组元素和它出现的次数,然后最后我...

weixin_39907220的博客 834

去除数组{1,3,1,4,2,3,6,1,5}中的重复项,存入一个新的数组,并从大到小排序.

import java.util.Arrays; public class work07 { public static void main(String[] args) { //【难】去除数组{1,3,1,4,2,3,6,1,5}中的重复项,存入一个新的数组,并从大到小排序. int []oldArr= new int[]{1,3,1,4,2,3,6,1,5}; int []newArr=deleteRepeatItem(oldArr); ...

玉麒麟 1069

删除已排序数组中的重复数

public class DuplicatedNumberHandler { public static void main(String[] orgs){ int[] arr = {1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,6}; DuplicatedNumberHandler handler = new DuplicatedNumberHandler()

fenglvming的专栏 647

容易 删除排序数组中的重复数字

Remove Duplicates from Sorted Array -- LeetCode 分类: LeetCode2014-02-27 05:12 4789人阅读 评论(5) 收藏 举报 leetcodejava面试链表数据结构 原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-arr

兔清风的博客 5454

剑指 Offer 03. 数组中重复的数字

剑指 Offer 03. 数组中重复的数字1.题目描述2.题解思路2.1 哈希表实现2.2 排序法实现2.3 最优解法 1.题目描述 找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi

weixin_50901244的博客 392

Java基础之:统计一个数组当中重复的元素个数,重复的元素分别是谁,重复元素的重复了多少次数组.........

条条大路通罗马这道题我是这样想的,既然是需要输出重复的次数并且输出重复的元素,那我是用到了我今天学到的一个Arrays工具类中的排序方法,先让它排升序。然后用循环再依次判断它们是否相等,如果相等就记录相等的次数,不相等就跳出循环然后在做判断。话不多说直接看代码。我这个方法思路只适用排完序的数组如果数组没有排序那么这个思路就行不通,所以要用这个方法的前提一点要把数组排序!...

m0_72176143的博客 1755
上一篇: 火龙果对提问正则表达式帖子的建议
下一篇: 微软把用户变成了傻瓜,却指责傻瓜不聪明
老紫竹
博客等级 码龄19年 1万+粉丝 1252原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址