11月26日 每日一题 leetcode 8. String to Integer (atoi) && 剑指offer 两个链表的第一个公共结点
String to Integer (atoi) 题目:
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
Input: “42”
Output: 42
Example 2:
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
Input: “4193 with words”
Output: 4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.
Example 4:
Input: “words and 987”
Output: 0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
Input: “-91283472332”
Output: -2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
解析:其实就是模拟,首先排除几种特殊情。
1)字符串为空,返回0
2)最开始不是以数字或者‘+’‘-’开头的,返回0
接下来就是找到合法的数字的区间,找到第一个不是0-9的字符的下标,这样就构成了一个合法的数字序列[first,end),然后就是进行累乘了,当溢出的时候,果断跳出去,然后根据相应的规则设定为INT_MAX还是INT_MIN。
代码:
class Solution {
public:
int myAtoi(string str)
{
int len = str.length();
if (len == 0) return 0;
bool ok = true;
int first_index = 0;
int end_index;
for (first_index = 0; first_index < len; first_index++)
{
if (str[first_index] == ' ') continue;
else if (str[first_index] == '-' || str[first_index] == '+' || (str[first_index] >= '0'&&str[first_index] <= '9')) break;
else ok = false;
}
if (!ok||first_index==len) return 0;
end_index = first_index + 1; // 左闭右开
while (str[end_index] >= '0'&&str[end_index] <= '9') ++end_index;
long long ans = 0;
std::vector<char> vec;
for (int i = first_index; i<end_index; i++) vec.push_back(str[i]);
typedef std::vector<char>::iterator Iter;
// Iter it = vec.begin();
// while(it!=vec.end())
// {
// cout<<*it;
// ++it;
// }
Iter it = vec.begin();
bool flag = 0; //'-'
if(*it == '-') flag = 1 ;
bool ok1 = true;
if(*it == '-' || *it == '+') ++it;
while (it != vec.end())
{
ans = ans * 10 + (*it - '0');
if (!flag&&ans > INT_MAX)
{
ok1 = false;
break;
}
if (flag&&-ans < INT_MIN)
{
ok1 = false;
break;
}
++it;
}
if (!ok1)
{
if (!flag) return INT_MAX;
else return INT_MIN;
}
if (str[first_index] == '-') ans = -ans;
cout << ans << endl;
if (ans < INT_MIN) ans = INT_MIN;
if (ans >= INT_MAX) ans = INT_MAX;
//cout << "INT_MAX "<<INT_MAX << endl;
return ans;
}
};
剑指offer 两个链表的第一个公共结点
题目:
输入两个链表,找出它们的第一个公共结点。
思路:
如果两个链表有公共节点,那么只有可能是这样情形,所以只要算出两个链表的差值,然后长的那个先移动差值,然后在慢慢比较,就能找到第一个公共的节点了。
代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2)
{
if(pHead1==NULL||pHead2==NULL) return NULL;
int len_1,len_2;
len_1 = len_2 = 0;
ListNode* pHead = pHead1;
while(pHead)
{
len_1++;
pHead = pHead->next;
}
pHead = pHead2;
while(pHead)
{
len_2++;
pHead = pHead->next;
}
int cnt = (len_1>len_2)?len_1-len_2:len_2-len_1;
bool ok = len_1>len_2;
if(ok)
{
while(cnt)
{
pHead1 = pHead1->next;
--cnt;
}
}
else
{
while(cnt)
{
pHead2 = pHead2->next;
--cnt;
}
}
while(pHead1&&pHead2&&pHead1!=pHead2)
{
pHead1 = pHead1->next;
pHead2 = pHead2->next;
}
return pHead1;
}
};