Skip to content

Commit 4533630

Browse files
authored
Merge pull request #269 from Kavisha20340/master
Added solution to Leetcode problem 1290
2 parents dd79296 + e0d1327 commit 4533630

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

leetcode/cpp/Stacks/682.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Problem 682 : Baseball Game(using stack)
2+
3+
class Solution {
4+
public:
5+
int calPoints(vector<string>& ops)
6+
{
7+
int result = 0;
8+
stack <int> score;
9+
for (auto x : ops)
10+
{
11+
if (x == "C" && !score.empty())
12+
score.pop();
13+
else if(x == "D" && !score.empty())
14+
score.push(2*score.top());
15+
else if(x == "+" && !score.empty())
16+
{
17+
int top1 = score.top();
18+
score.pop();
19+
int top2 = score.top();
20+
score.push(top1);
21+
score.push((top1+top2));
22+
}
23+
else
24+
score.push(stoi(x)); // convert from string to int then push on stack
25+
}
26+
while(!score.empty())
27+
{
28+
result += score.top();
29+
score.pop();
30+
}
31+
return result;
32+
}
33+
};

leetcode/cpp/linkedlist/1290.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
int getDecimalValue(ListNode* head) {
14+
int answer = 0;
15+
vector<int> v;
16+
while(head){
17+
v.push_back(head->val);
18+
head = head->next;
19+
}
20+
reverse(v.begin(),v.end());
21+
for (int i = 0; i < v.size(); i++)
22+
if(v.at(i))
23+
answer += pow(2,i);
24+
return answer;
25+
}
26+
};
27+
28+

0 commit comments

Comments
 (0)