leetcode 137. Single Number II

本文介绍了一种高效的算法,用于从整数数组中找出只出现一次的元素,其余元素均出现三次。该方法通过布尔代数原理设计计数器,实现线性时间复杂度,并避免额外内存使用。

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


没有想到方法,看了别人的一些方法受益匪浅,记录之

this kind of question the key idea is design a counter that record state. the problem can be every one occurs K times except one occurs M times. for this question, K =3 ,M = 1(or 2) .
so to represent 3 state, we need two bit. let say it is a and b, and c is the incoming bit.
then we can design a table to implement the state move.

current   incoming  next
a b            c    a b
0 0            0    0 0
0 1            0    0 1
1 0            0    1 0
0 0            1    0 1
0 1            1    1 0
1 0            1    0 0

like circuit design, we can find out what the next state will be with the incoming bit.( we only need find the ones)
then we have for a to be 1, we have

    current   incoming  next
    a b            c    a b
    1 0            0    1 0
    0 1            1    1 0

and this is can be represented by

a=a&~b&~c + ~a&b&c

and b can do the same we , and we find that

b= ~a&b&~c+~a&~b&c

and this is the final formula of a and b and just one of the result set, because for different state move table definition, we can generate different formulas, and this one is may not the most optimised. as you may see other's answer that have a much simple formula, and that formula also corresponding to specific state move table. (if you like ,you can reverse their formula to a state move table, just using the same way but reversely)

for this questions we need to find the except one
as the question don't say if the one appears one time or two time ,
so for ab both

01 10 => 1
00 => 0

we should return a|b;
this is the key idea , we can design any based counter and find the occurs any times except one .
here is my code. with comment.

public class Solution {
    
    public int singleNumber(int[] nums) {
        //we need to implement a tree-time counter(base 3) that if a bit appears three time ,it will be zero.
        //#curent  income  ouput
        //# ab      c/c       ab/ab
        //# 00      1/0       01/00
        //# 01      1/0       10/01
        //# 10      1/0       00/10
        // a=~abc+a~b~c;
        // b=~a~bc+~ab~c;
        int a=0;
        int b=0;
        for(int c:nums){
            int ta=(~a&b&c)|(a&~b&~c);
            b=(~a&~b&c)|(~a&b&~c);
            a=ta;
        }
        //we need find the number that is 01,10 => 1, 00 => 0.
        return a|b;
        
    }
}

this is a general solution . and it comes from the Circuit Design on course digital logic.

另外的一个解释

The code seems tricky and hard to understand at first glance.
However, if you consider the problem in Boolean algebra form, everything becomes clear.

What we need to do is to store the number of '1's of every bit. Since each of the 32 bits follow the same rules, we just need to consider 1 bit. We know a number appears 3 times at most, so we need 2 bits to store that. Now we have 4 state, 00, 01, 10 and 11, but we only need 3 of them.

In this solution, 00, 01 and 10 are chosen. Let 'ones' represents the first bit, 'twos' represents the second bit. Then we need to set rules for 'ones' and 'twos' so that they act as we hopes. The complete loop is 00->10->01->00(0->1->2->3/0).

  • For 'ones', we can get 'ones = ones ^ A[i]; if (twos == 1) then ones = 0', that can be tansformed to 'ones = (ones ^ A[i]) & ~twos'.

  • Similarly, for 'twos', we can get 'twos = twos ^ A[i]; if (ones* == 1) then twos = 0' and 'twos = (twos ^ A[i]) & ~ones'. Notice that 'ones*' is the value of 'ones' after calculation, that is why twos is
    calculated later.


Here is another example. If a number appears 5 times at most, we can write a program using the same method. Now we need 3 bits and the loop is 000->100->010->110->001. The code looks like this:

int singleNumber(int A[], int n) {
	int na = 0, nb = 0, nc = 0;
	for(int i = 0; i < n; i++){
		nb = nb ^ (A[i] & na);
		na = (na ^ A[i]) & ~nc;
		nc = nc ^ (A[i] & ~na & ~nb);
	}
	return na & ~nb & ~nc;
}

Or even like this:

int singleNumber(int A[], int n) {
	int twos = 0xffffffff, threes = 0xffffffff, ones = 0;
	for(int i = 0; i < n; i++){
		threes = threes ^ (A[i] & twos);
		twos = (twos ^ A[i]) & ~ones;
		ones = ones ^ (A[i] & ~twos & ~threes);
	}
	return ones;
}

I hope all these above can help you have a better understand of this problem.


内容概要:本文详细介绍了一个基于秃鹰搜索算法(BES)优化最小二乘支持向量机(LSSVM)的多特征分类预测项目,涵盖从理论原理、模型架构、代码实现到GUI界面设计的完整流程。项目通过BES算法自动优化LSSVM的关键参数(如正则化参数C和核函数参数gamma),提升模型在高维、多特征数据下的分类精度与泛化能力。结合特征工程、交叉验证、数据增强等技术,有效应对过拟合与参数调优难题,并通过混淆矩阵、ROC曲线、t-SNE可视化等多种方式实现结果解释与模型评估。项目还提供了完整的目录结构、模块化代码封装、并行计算支持及可扩展的部署架构,适用于金融风控、医疗诊断、工业故障检测等多个领域。; 适合人群:具备一定Python编程基础和机器学习知识的研发人员、数据科学家及工程技术人员,尤其适合从事智能算法开发、模型优化与实际工程落地的相关从业者;工作年限建议在1-5年之间。; 使用场景及目标:①在高维多特征数据场景中实现高精度分类预测;②解决传统LSSVM人工调参困难的问题,实现参数自动寻优;③构建可解释、可可视化、可部署的智能分类系统,支持金融、医疗、工业等领域的智能决策应用;④学习如何将智能优化算法(如BES)与经典机器学习模型(如LSSVM)融合并实现端到端项目开发。; 阅读建议:建议读者结合文中提供的完整代码进行实践操作,重点关注BES优化算法的实现逻辑、LSSVM的训练流程以及GUI界面的集成方式。在学习过程中,可尝试更换数据集、调整参数范围或引入其他优化算法进行对比实验,以深入理解模型性能变化机制。同时,建议关注项目部署与可扩展性设计,为后续工程化应用打下基础。
【源码免费下载链接】:https://renmaiwang.cn/s/yzxo9 在本项目中,我们深入研究了利用WPF构建登录界面的技术,并探讨了如何实现与 WinForms 主窗口之间的平滑切换效果。作为.NET Framework的一部分,WPF提供了丰富的用户界面设计功能;而 WinForm 是一个成熟的UI框架,在两者协同工作时可以充分发挥各自优势。为了创建登录页面,我们需要使用XAML来定义布局和控件,包括文本框、密码输入框以及按钮等基本元素。例如,在实现简单的登录界面时,我们可以将用户名、密码字段分别放置在两个文本框中,并通过一个按钮触发登录操作。具体的代码实现可以通过以下段落展示:```xml[此处应包含XAML代码片段]``` 在后台脚本部分,我们需要为按钮的点击事件编写逻辑来验证用户输入的安全性。例如,在上述示例中,我们假设已经存在名为 WinFormApp 的 WinForms 主界面类,并通过如下代码实现了功能:```csharp[此处应包含C#代码片段]``` 值得注意的是,WPF窗口可以与 WinForms 窗口实现交互切换,这种设计不仅提升了用户体验,还提供了良好的扩展性和可维护性。在实际开发过程中,建议将用户凭证存储在安全的位置,并采用数据绑定和 MVVM 模式来增强代码的可管理性。此外,在项目中我们巧妙地整合了两种UI设计框架,使得最终应用既具备美观界面又展现出强大功能。通过这种混合开发模式,我们可以快速构建出满足多种需求的应用程序。 为方便学习者深入研究,压缩包文件 TEST_WPF 登录打开 WinForm 主界面 中提供了一个完整的WPF与WinForm混合开发案例,其中详细展示了项目的实现过程和相关技术细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值