forked from ruchika-jain/Competitive_coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp
More file actions
65 lines (58 loc) · 1.46 KB
/
cp
File metadata and controls
65 lines (58 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
LC 1
Two integers adding to a sum and return the indeces
#include<bits/stdc++.h>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>res;
unordered_map<int,int>m;
for(int i=0;i<nums.size();i++){
int c=target-nums[i];
if(m.find(c)!=m.end()){
res.push_back(m[c]);
res.push_back(i);
break;
}
m[nums[i]]=i;
}
return res;
}
};
Corner case- Check that the complement is not itself and if it is same, then also check that m[c]!=if
LC 7
Reverse integer wiyhin 2^32 if more than that return 0
#include<bits/stdc++.h>
class Solution {
public:
int reverse(int x) {
int temp=0;
int d;
while(x!=0){
d=x%10;
x/=10;
if(temp>INT_MAX/10||(temp==INT_MAX/10 && d>7))return 0;
if(temp<INT_MIN/10||(temp==INT_MIN/10 && d<-8))return 0;
temp=temp*10+d;
}
return temp;
}
};
Alternate-Converting to string
class Solution {
public:
int reverse(long x) {
long temp=0;
long v=abs(x);
string s=to_string(v);
string rs=" ";
for(int i=s.length()-1;i>=0;i--)
rs+=s[i];
temp=stol(rs);
if((temp>pow(2,31))||(x>0 && temp==pow(2,31)))
return 0;
else if(x>0)
return temp;
else
return -temp;
}
};