class Solution {
public:
int getDecimalValue(ListNode* head) {
vector<int>num;
while(head)
{
num.push_back(head->val);
head=head->next;
}
int l=num.size();
int m=0;
for(int x=l-1;x>=0;--x)
{
m+=num[x]*pow(2,l-1-x);
}
return m;
}
};
开始五分钟写出了这一段代码,结果发现结果并不好。
所以我想了下改进的思路。
class Solution {
public:
int getDecimalValue(ListNode* head) {
int m=0;
while(head)
{
m=m*2+head->val;
head=head->next;
}
return m;
}
};
又是五分钟~哈哈!(碰撞)
加油!