第一題:整數(shù)反轉(zhuǎn)
給出一個(gè) 32 位的有符號整數(shù)胆建,你需要將這個(gè)整數(shù)中每位上的數(shù)字進(jìn)行反轉(zhuǎn)烤低。
示例 1:
輸入:123輸出:321
示例 2:
輸入: -123輸出: -321
示例 3:
輸入:120輸出:21
示例 4:
輸入:1534236469輸出:0
示例 5:
輸入: -2147483648輸出:0
注意:
假設(shè)我們的環(huán)境只能存儲得下 32 位的有符號整數(shù)肘交,則其數(shù)值范圍為[?2^31, 2^31 ? 1]笆载。請根據(jù)這個(gè)假設(shè),如果反轉(zhuǎn)后整數(shù)溢出那么就返回 0涯呻。
public int Reverse(int x) {
? ? ? ? if (x == int.MinValue)
? ? ? ? ? ? return 0;
? ? ? ? long result = 0;
? ? ? ? int negative = x < 0 ? -1 : 1;
? ? ? ? x = negative * x;
? ? ? ? Queue<int> q = new Queue<int>();
? ? ? ? while (x != 0)
? ? ? ? {
? ? ? ? ? ? q.Enqueue(x % 10);
? ? ? ? ? ? x = x/10;
? ? ? ? }
? ? ? ? while (q.Count != 0)
? ? ? ? {
? ? ? ? ? ? result += q.Dequeue()*(long) Math.Pow(10, q.Count);
? ? ? ? ? ? if (negative == 1 && result > int.MaxValue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? result = 0;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? if (negative == -1 && result*-1 < int.MinValue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? result = 0;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return (int)result*negative;? ? ? ?
? ? }
初始值設(shè)為0凉驻,通過設(shè)置一個(gè)隊(duì)列,將負(fù)數(shù)變?yōu)檎龜?shù)复罐,因?yàn)檎龜?shù)可以統(tǒng)一進(jìn)行運(yùn)算處理涝登,最后再把負(fù)號加上樟凄。
第二題字符串轉(zhuǎn)換整數(shù)
請你來實(shí)現(xiàn)一個(gè) atoi 函數(shù)跟磨,使其能將字符串轉(zhuǎn)換成整數(shù)徘跪。
首先火架,該函數(shù)會根據(jù)需要丟棄無用的開頭空格字符嗅战,直到尋找到第一個(gè)非空格的字符為止寄悯。
當(dāng)我們尋找到的第一個(gè)非空字符為正或者負(fù)號時(shí)招刨,則將該符號與之后面盡可能多的連續(xù)數(shù)字組合起來暖释,作為該整數(shù)的正負(fù)號戚炫;假如第一個(gè)非空字符是數(shù)字剑刑,則直接將其與之后連續(xù)的數(shù)字字符組合起來,形成整數(shù)。
該字符串除了有效的整數(shù)部分之后也可能會存在多余的字符施掏,這些字符可以被忽略钮惠,它們對于函數(shù)不應(yīng)該造成影響。
注意:假如該字符串中的第一個(gè)非空格字符不是一個(gè)有效整數(shù)字符七芭、字符串為空或字符串僅包含空白字符時(shí)素挽,則你的函數(shù)不需要進(jìn)行轉(zhuǎn)換。
在任何情況下狸驳,若函數(shù)不能進(jìn)行有效的轉(zhuǎn)換時(shí)毁菱,請返回 0。
說明:
假設(shè)我們的環(huán)境只能存儲 32 位大小的有符號整數(shù)锌历,那么其數(shù)值范圍為 [?2^31, 2^31? 1]贮庞。如果數(shù)值超過這個(gè)范圍,請返回 INT_MAX (2^31? 1) 或 INT_MIN (?2^31) 究西。
示例 1:
輸入: "42"
輸出: 42
示例 2:
輸入: "? -42"
輸出: -42
解釋: 第一個(gè)非空白字符為 '-', 它是一個(gè)負(fù)號窗慎。
我們盡可能將負(fù)號與后面所有連續(xù)出現(xiàn)的數(shù)字組合起來,最后得到 -42 卤材。
示例 3:
輸入: "4193 with words"
輸出: 4193
解釋: 轉(zhuǎn)換截止于數(shù)字 '3' 遮斥,因?yàn)樗南乱粋€(gè)字符不為數(shù)字。
示例 4:
輸入: "words and 987"
輸出: 0
解釋: 第一個(gè)非空字符是 'w', 但它不是數(shù)字或正扇丛、負(fù)號术吗。
因此無法執(zhí)行有效的轉(zhuǎn)換。
示例 5:
輸入: "-91283472332"
輸出: -2147483648
解釋: 數(shù)字 "-91283472332" 超過 32 位有符號整數(shù)范圍帆精。
因此返回 INT_MIN (?231) 较屿。
示例 6:
輸入: "? 0000000000012345678"
輸出: 12345678
示例 7:
輸入: "20000000000000000000"
輸出: 2147483647
public int Atoi(string str) {
? ? ? ? str = str.Trim();
? ? ? ? if (string.IsNullOrEmpty(str))
? ? ? ? ? ? return 0;
? ? ? ? if (str[0] != '-' && str[0] != '+')
? ? ? ? {
? ? ? ? ? ? if (str[0] < '0' || str[0] > '9')
? ? ? ? ? ? ? ? return 0;
? ? ? ? }
? ? ? ? int negative = 1;
? ? ? ? long result = 0;
? ? ? ? Queue<int> q = new Queue<int>();
? ? ? ? for (int i = 0; i < str.Length; i++)
? ? ? ? {
? ? ? ? ? ? if (str[i] == '-' && i == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? negative = -1;
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if (str[i] == '+' && i == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? if (str[i] < '0' || str[i] > '9')
? ? ? ? ? ? {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? q.Enqueue(str[i] - '0');
? ? ? ? }
? ? ? ? while (q.Count != 0)
? ? ? ? {
? ? ? ? ? ? int i = q.Dequeue();
? ? ? ? ? ? //去掉隊(duì)列前端的零
? ? ? ? ? ? if (i == 0 && result == 0)
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? // 返回超過位數(shù)的數(shù)字
? ? ? ? ? ? if (negative == 1 && q.Count > 10)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return int.MaxValue;
? ? ? ? ? ? }
? ? ? ? ? ? if (negative == -1 && q.Count > 10)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return int.MinValue;
? ? ? ? ? ? }
? ? ? ? ? ? result += i * (long)Math.Pow(10, q.Count);
? ? ? ? ? ? if (negative == 1 && result > int.MaxValue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return int.MaxValue;
? ? ? ? ? ? }
? ? ? ? ? ? if (negative == -1 && result * -1 < int.MinValue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return int.MinValue;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return (int)result * negative;? ? ? ?
? ? }
將字符串存儲在一個(gè)隊(duì)列中,遍歷隊(duì)列:如果是負(fù)數(shù)卓练,將negative置為-1隘蝎,否則的話不變。然后統(tǒng)計(jì)字符串的位數(shù)襟企,賦值給i嘱么,最后根據(jù)i的范圍來返回值。
第三題 回文數(shù)
判斷一個(gè)整數(shù)是否是回文數(shù)顽悼÷瘢回文數(shù)是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數(shù)。
示例 1:
輸入:121輸出:true
示例 2:
輸入: -121輸出:false解釋: 從左向右讀, 為 -121蔚龙。 從右向左讀, 為121- 冰评。因此它不是一個(gè)回文數(shù)。
示例 3:
輸入:10輸出:false解釋: 從右向左讀, 為01府蛇。因此它不是一個(gè)回文數(shù)集索。
public bool IsPalindrome(int x) {
? ? ? ? if (x < 0)
? ? ? ? ? ? return false;
? ? ? ? int digit = 1;
? ? ? ? while (x / digit >= 10)
? ? ? ? {
? ? ? ? ? ? digit = digit * 10;
? ? ? ? }
? ? ? ? while (x > 0)
? ? ? ? {
? ? ? ? ? ? int left = x % 10;
? ? ? ? ? ? int right = x / digit;
? ? ? ? ? ? if (left != right)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? x = (x % digit) / 10;
? ? ? ? ? ? digit = digit / 100;
? ? ? ? }
? ? ? ? return true;
先將位數(shù)digit初始化為1,將字符串分為left和right,從中間向兩邊遍歷搜索直到left與right不相等為止务荆。