8000 Merge branch 'master' of https://github.com/shivamkumar177/LeetcodeMay · Ibhrahimazzad/LeetcodeMay@9036e00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9036e00

Browse files
2 parents d6e8fe8 + ddc7483 commit 9036e00

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

WEEK 1/Question 6 Majority Element.txt renamed to WEEK 1/Question 6 Majority Element.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ class Solution {
1919
}
2020
return ans;
2121
}
22-
};
22+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int singleNonDuplicate(vector<int>& nums) {
4+
if(nums.size()==1)return nums[0];
5+
for(int i=0,j=1;i<nums.size();i+=2)
6+
{
7+
if(nums[i]!=nums[j])return nums[i];
8+
j+=2;
9+
}
10+
return nums[nums.size()-1];
11+
}
12+
};

WEEK 2/Question 6 Remove k Digits.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string removeKdigits(string num, int k) {
4+
if(num.length()==k)return "0";
5+
stack<char> s;
6+
for(char c: num)
7+
{
8+
while(!s.empty()&&k>0&&s.top()>c)s.pop(),k--;
9+
if(!s.empty()||c!='0')s.push(c);
10+
}
11+
while(!s.empty()&&k--)
12+
{
13+
s.pop();
14+
}
15+
int n=num.length();
16+
if(s.empty())return "0";
17+
while(!s.empty())
18+
{
19+
num[n-1]=s.top();
20+
s.pop();
21+
n--;
22+
}
23+
return num.substr(n);
24+
}
25+
};

0 commit comments

Comments
 (0)
0