每日刷題 有效數字

2021-09-27 08:06:00 字數 1783 閱讀 2847

驗證給定的字串是否可以解釋為十進位制數字。

說明: 我們有意將問題陳述地比較模糊。在實現**之前,你應當事先思考所有可能的情況。這裡給出乙份可能存在於有效十進位制數字中的字元列表:

數字 0-9

指數 - 「e」

正/負號 - 「+」/"-"

小數點 - 「.」

當然,在輸入中,這些字元的上下文也很重要。

方法一:直接法

依據題目的要求,對字串進行遍歷,當目前字元不滿足要求時,返回 false.

給出一些測試樣例:

test(1, "123", true);

test(2, " 123 ", true);

test(3, "0", true);

test(4, "0123", true); //cannot agree

test(5, "00", true); //cannot agree

test(6, "-10", true);

test(7, "-0", true);

test(8, "123.5", true);

test(9, "123.000000", true);

test(10, "-500.777", true);

test(11, "0.0000001", true);

test(12, "0.00000", true);

test(13, "0.", true); //cannot be more disagree!!!

test(14, "00.5", true); //strongly cannot agree

test(15, "123e1", true);

test(16, "1.23e10", true);

test(17, "0.5e-10", true);

test(18, "1.0e4.5", false);

test(19, "0.5e04", true);

test(20, "12 3", false);

test(21, "1a3", false);

test(22, "", false);

test(23, " ", false);

test(24, null, false);

test(25, ".1", true); //ok, if you say so

test(26, ".", false);

test(27, "2e0", true); //really?!

test(28, "+.8", true);

test(29, " 005047e+6", true); //damn = =|||

**:

class

solution

else

if( s[index]

=='.')if

(!dot_flag &&

!e_flag)

dot_flag++

;else

return

false

;else

if( s[index]

>=

'0'&& s[index]

<=

'9')

num_flag++

;else

return

false

;

index++;}

return num_flag !=0;

}};

方法二:有限狀態自動機。

LintCode刷題 有效數字

今天第一次是跟隨乙個博主學習足跡知道有lintcode 和leetcode這兩個刷題 專為學程式設計的同志練手,我希望我可以每天堅持刷一題!題目 有效數字 要求 希望可以得到如下效果 0 true 0.1 true abc false 1 a false 2e10 true 程式 public cl...

每日刷題 有效的數獨

判斷乙個 9x9 的數獨是否有效。只需要根據以下規則,驗證已填入的數字是否有效即可。示例 例 1 輸入 5 3 7 6 1 9 5 9 8 6 8 6 3 4 8 3 1 7 2 6 6 2 8 4 1 9 5 8 7 9 輸出 true 例 2 輸入 8 3 7 6 1 9 5 9 8 6 8 6...

每日刷題 打家劫舍

你是乙個專業的小偷,計畫偷竊沿街的房屋。每間房內都藏有一定的現金,影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警。給定乙個代表每個房屋存放金額的非負整數陣列,計算你在不觸動警報裝置的情況下,能夠偷竊到的最高金額。示例 例 1 輸入...