136. Single Number

本文介绍了一种解决LeetCode单数查找问题的算法,利用异或运算特性,实现线性时间复杂度O(n)与常数级空间复杂度O(1)。适用于寻找数组中仅出现一次的元素。

题目:

Given an array of integers, every element appears twice except for one. Find that single one.

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

链接: http://leetcode.com/problems/single-number/

题解:

位运算,用异或处理掉出现两次的数字即可。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public int singleNumber(int[] A) {
        int result = 0;
        
        for(int i : A){
            result ^= i;
        }
        
        return result;
    }
}

 

Update:

public class Solution {
    public int singleNumber(int[] nums) {
        if(nums == null || nums.length == 0)
            return 0;
        int res = 0;
        
        for(int i : nums)
            res ^= i;
            
        return res;
    }
}

 

二刷:

使用异或。a ^ b ^ a = b,  0 ^ b = b

Java:

Time Complexity - O(n), Space Complexity - O(1)。

 

public class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for (int i : nums) {
            res ^= i;
        }
        return res;
    }
}

 

三刷:

Java:

public class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for (int i : nums) {
            res ^= i;
        }
        return res;
    }
}

 

 

测试:

转载于:https://www.cnblogs.com/yrbbest/p/4438787.html

### C# `Convert.ToSingle` 方法使用说明 #### 方法概述 `Convert.ToSingle` 是 .NET Framework 提供的一个静态方法,用于将指定类型的数值转换为单精度浮点数 (`float`)。此方法能够处理多种输入类型并返回相应的 `float` 值。 #### 参数返回值 该方法接受一个参数,通常是一个实现了 `IConvertible` 接口的类型实例,如整数、字符串或其他数值类型,并尝试将其转换成 `float` 类型[^2]。 #### 使用示例 以下是几个常见的使用场景: ```csharp // 整数转浮点数 int intValue = 10; float resultFloatFromInt = Convert.ToSingle(intValue); Console.WriteLine($"Integer to Float: {resultFloatFromInt}"); // 输出 Integer to Float: 10.0 // 字符串转浮点数 string stringValue = "123.45"; float resultFloatFromString = Convert.ToSingle(stringValue); Console.WriteLine($"String to Float: {resultFloatFromString}"); // 输出 String to Float: 123.45 ``` #### 特殊情况处理 当遇到无法解析的数据时,`Convert.ToSingle` 可能会抛出异常。对于可能包含无效字符或格式错误的字符串,在执行转换前应先验证数据的有效性。 #### 注意事项 - 对于超出范围的情况(即源值太大或太小),可能会得到正负无穷大或者零的结果。 - 如果传入的是 `null` 或者空字符串,则默认返回零(`0.0f`)。 - 当处理来自用户的输入或者其他不可信来源的数据时,建议捕获潜在的异常以防止程序崩溃。 #### 错误处理示范 为了更安全地使用 `Convert.ToSingle`,可以在实际项目中加入异常捕捉机制来应对可能出现的问题: ```csharp try { string userInput = Console.ReadLine(); float convertedNumber = Convert.ToSingle(userInput); Console.WriteLine($"Converted number is :{convertedNumber}"); } catch (FormatException ex){ Console.WriteLine("The input format was not correct."); } catch (OverflowException ox){ Console.WriteLine("The value provided cannot be represented as a single precision floating point number."); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值