my_leetcode/c_sharp/my_leetcode/Solution.cs

39 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace my_leetcode
{
internal class Solution
{
/// <summary>
/// 268.丢失的数字
/// </summary>
/// <param name="nums">数组</param>
/// <returns>丢失的数字</returns>
public int MissingNumber(int[] nums)
{
return new int[4] { nums.Length, 1, nums.Length + 1, 0 }[nums.Length & 3] ^ nums.Aggregate(0, (o, n) => o ^ n);
}
/// <summary>
/// 69.Sqrt(x)
/// </summary>
/// <param name="x">x</param>
/// <returns>平方根</returns>
public int MySqrt(int x)
{
double xHalf = 0.5 * x;
long i = BitConverter.DoubleToInt64Bits(x);
i = 0x1FF7A3BEA91D9B1B + (i >> 1);
double f = BitConverter.Int64BitsToDouble(i);
f = f * 0.5 + xHalf / f;
f = f * 0.5 + xHalf / f;
f = f * 0.5 + xHalf / f;
return (int)f;
}
}
}