这算O(n)还是O(logn)?
来源:互联网 发布:淘宝回收充值卡安全吗 编辑:程序博客网 时间:2024/05/17 03:00
转换字符串为浮点数
namespace ConsoleApplication1{ class TestFaiureException : Exception { public TestFaiureException(string s) : base(s) { } } class MyUtils { public static void _Main(string[] args) { string[] testData = new string[] { "123", "-123", "123.1", "-123.1", "0", "0.1", "1.2.3", "-.3", "0.a", ".1" }; double[] expected = new double[] { 123, -123, 123.1, -123.1, 0, 0.1, 0, 0, 0, 0 }; try { delegateConvertToFloat dctf = ConvertToFloat; for (int i = 0; i < testData.Length; i++) { TestConvertToFloat(dctf, testData[i], expected[i]); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } delegate double delegateConvertToFloat(string s); private static void TestConvertToFloat(delegateConvertToFloat func, string input, double expected) { try { double actual = func(input); if (expected == actual) { return; } else { throw new TestFaiureException(string.Format("expected: {0}, actual: {1}", expected.ToString(), actual)); } } catch (FormatException ex) { Console.WriteLine(ex.Message); } catch (TestFaiureException ex) { Console.WriteLine(ex.Message); } } private static double ConvertToFloat(string s) { if (string.IsNullOrEmpty(s)) { throw new ArgumentNullException("s"); } string pattern = @"^[+-]?\d+(\.\d+)?$"; if (!Regex.IsMatch(s, pattern, RegexOptions.Singleline)) { throw new FormatException("not legal digit: " + s); } string digits; bool isNagative = false; if (s.StartsWith("+")) { digits = s.Substring(1); } else if (s.StartsWith("-")) { digits = s.Substring(1); isNagative = true; } else { digits = s; } int dot = digits.IndexOf("."); int ldot; int rdot; if (dot == -1) { dot = digits.Length; ldot = digits.Length - 1; rdot = digits.Length; } else { ldot = dot - 1; rdot = dot + 1; } int len = digits.Length; double lResult = 0; double rResult = 0; while (ldot >= 0 || rdot < len) { if (ldot >= 0) { lResult = lResult + (digits[ldot] - '0') * Math.Pow(10, (dot - 1) - ldot); ldot--; } if (rdot < len) { rResult = rResult + (digits[rdot] - '0') * Math.Pow(10, dot - rdot); rdot++; } } return isNagative ? (-1) * (lResult + rResult) : (lResult + rResult); } }}
这才是真正的O(N)
class StringToFloat { private static bool InternalValidate(string strFloat) { return true; } public static double ConvertTodouble(string s) { if (!InternalValidate(s)) { throw new FormatException(s); } double sum1 = 0; double sum2 = 0; bool meetDot = false; int dotIndex = 1; for (int i = 0; i < s.Length; i++) { if (s[i] == '.') { meetDot = true; continue; } if (!meetDot) { sum1 = sum1 * 10 + (s[i] - '0'); continue; } if (meetDot) { sum2 = sum2 + (s[i] - '0') * Math.Pow(0.1, dotIndex); dotIndex++; continue; } } return sum1 + sum2; } }
- 这算O(n)还是O(logn)?
- LCA <O(N logN, O(logN)>
- O(logn*2^logn)和O(n*logn)算法
- LCS O(n logn)解法
- 归并排序 O(N*logN)
- 把O(n)变成O(logn)
- o(1), o(n), o(logn), o(nlogn)
- poj-3903 Stock Exchang O(N*logN)
- [LeetCode] Pow(x,n) O(logN)
- COGS 1473 O(N*logN) 高精乘 FFT
- closest pair of points(O(n*logn*logn)) solution
- 杭电1257(最长递增子序列O(N*N)+O(N*logN))
- O(logn)
- 二分查找O(logn)
- 快速幂 O(logn)
- 一个关于O(N*logN)耗时下限的理论
- 今天才搞清楚排序算法的O(N*logN)是什么意思
- 高级排序:希尔排序 O(N*(logN)^2)
- java.io.FileNotFoundException
- 下代MapReduce预览 节点数翻倍最大达10000
- Struts首例:界面
- Linux进程间通信深刻理解
- 没有任何一块领土是靠谈判获得的
- 这算O(n)还是O(logn)?
- Struts首例:ActionForm
- 实训第一天 重构银行
- HTTP协议详解(真的很经典)
- 将 Windows IPC 应用程序移植到 Linux,第 2 部分: 信号量和事件
- 将 Windows IPC 应用移植到 Linux,第 1 部分: 进程和线程
- Struts首例:Action
- JAVA图形编程入门(连载3)
- Struts首例:web.xml