136. Single Number

本文介绍了两种解决单例问题的方法:一种是通过排序并遍历数组来找出仅出现一次的整数;另一种则是利用位运算XOR的独特性质,高效地找到目标数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一个int型数组,除了其中一个元素以外,其他的均出现两次。

 

My Solution:

 1     public int singleNumber(int[] nums) {
 2         //Sort this array in an ascending order.
 3         Arrays.sort(nums);
 4         int x = 0;
 5         /*    Elements will appear in pair except for one.
 6          *     Increase the subscript of this array progressively by 2.
 7          *     The one unequal to its next or the last one left would be returned.
 8          */
 9         for(int i = 0; i<nums.length ;i += 2){
10             if(i + 1 == nums.length || nums[i] != nums[i+1]){
11                 x = nums[i];
12                 break;
13             }
14         }
15         return x;
16     }

 

 

Others' Solution:

 1 /*  We use bitwise XOR to solve this problem :
 2 
 3     First , we have to know the bitwise XOR in java
 4 
 5     0 ^ N = N
 6     N ^ N = 0
 7     So..... if N is the single number
 8 
 9     N1 ^ N1 ^ N2 ^ N2 ^..............^ Nx ^ Nx ^ N
10 
11     = (N1^N1) ^ (N2^N2) ^..............^ (Nx^Nx) ^ N
12 
13     = 0 ^ 0 ^ ..........^ 0 ^ N
14 
15     = N 
16  */
17 
18 
19 public int singleNumber(int[] nums) {
20     int ans =0;
21     
22     int len = nums.length;
23     for(int i=0;i!=len;i++)
24         ans ^= nums[i];
25     
26     return ans;
27 }

 

posted on 2017-01-31 14:59 LuoJunC 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/luojunc/p/6358823.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、付费专栏及课程。

余额充值