Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cecc82e

Browse files
Improve C++ solution of problem #1290
1 parent afddccb commit cecc82e

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

‎1290/solution1.cpp‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
class Solution {
1414
public:
1515
int getDecimalValue(ListNode* head) {
16-
vector<int> binary;
17-
while(head) {
18-
binary.push_back(head->val);
19-
head = head->next;
16+
ListNode *cur = head;
17+
int ans = 0;
18+
while(cur) {
19+
ans <<= 1;
20+
ans |= cur->val;
21+
cur = cur->next;
2022
}
21-
22-
int result = 0;
23-
for(int i = 0; i < binary.size(); i++) {
24-
result += binary[i] * pow(2, binary.size() - i - 1);
25-
}
26-
return result;
23+
return ans;
2724
}
2825
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /