Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n=nums.size();
vector<vector<int>> res;
for(int i=0;i<n-2;i++){
if(i>0 && (nums[i]==nums[i-1]) )continue;
int l=i+1, r= n-1;
while(l<r){
int sum =nums[i]+nums[l]+nums[r];
if(sum<0) l++;
else if(sum>0)r--;
else {
res.push_back(vector<int>{nums[i],nums[l],nums[r]});
while(l+1<r && nums[l]==nums[l+1])l++;
while(l<r-1 && nums[r]==nums[r-1]) r--;
l++; r--;
}
}
}
return res;
}

vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n=nums.size();
vector<vector<int>> res;
for(int i=0;i<n-2;i++){
if(i>0 && (nums[i]==nums[i-1]) )continue;
int l=i+1, r= n-1;
while(l<r){
int sum =nums[i]+nums[l]+nums[r];

if(sum<0) l++;
else if(sum>0)r--;
else {
res.push_back(vector<int>{nums[i],nums[l],nums[r]});
while(l+1<r && nums[l]==nums[l+1])l++;
while(l<r-1 && nums[r]==nums[r-1]) r--;
l++; r--;
}
}
}

return res;
}

Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
if(nums.empty()) return res;
int n = nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
long target2 = long(target) - long(nums[j]) - long(nums[i]);
int left = j+1;
int right = n-1;
while(left < right){
long twoSum = nums[left] + nums[right];
if(left < right && twoSum < target2){
left++;
}else if(left < right && twoSum > target2){
right--;
}else{
vector<int> storeAns(4,0);
storeAns[0] = nums[i];
storeAns[1] = nums[j];
storeAns[2] = nums[left];
storeAns[3] = nums[right];
res.push_back(storeAns);
while(left < right && nums[left] == storeAns[2]) ++left;
while(left < right && nums[right] == storeAns[3]) --right;
}
}
while(j+1 < n && nums[j+1] == nums[j]) ++j;
}
while(i+1 < n && nums[i+1] == nums[i]) ++i;
}
return res;
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {

vector<vector<int>> res;

if(nums.empty()) return res;

int n = nums.size();
sort(nums.begin(),nums.end());

for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){

long target2 = long(target) - long(nums[j]) - long(nums[i]);
int left = j+1;
int right = n-1;

while(left < right){
long twoSum = nums[left] + nums[right];
if(left < right && twoSum < target2){
left++;
}else if(left < right && twoSum > target2){
right--;
}else{
vector<int> storeAns(4,0);
storeAns[0] = nums[i];
storeAns[1] = nums[j];
storeAns[2] = nums[left];
storeAns[3] = nums[right];
res.push_back(storeAns);

while(left < right && nums[left] == storeAns[2]) ++left;
while(left < right && nums[right] == storeAns[3]) --right;

}
}
while(j+1 < n && nums[j+1] == nums[j]) ++j;
}
while(i+1 < n && nums[i+1] == nums[i]) ++i;
}

return res;

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
int goodIndx = n-1;
for(int i=n-2;i>=0;i--){
if(i+nums[i]>=goodIndx){
goodIndx = i;
}
}
if(goodIndx == 0){
return true;
}else{
return false;
}
}
};
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
int goodIndx = n-1;
for(int i=n-2;i>=0;i--){

if(i+nums[i]>=goodIndx){
goodIndx = i;
}

}

if(goodIndx == 0){
return true;
}else{
return false;
}
}
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
int removeDuplicates(vector<int>& nums) {
int k = 0,n = nums.size();
for(int i=0;i<n;i++){
int j = i;
while(j<n and nums[i]==nums[j]){
j++;
}
nums[k++] = nums[i];
i = j -1;
}
return k;
}
int removeDuplicates(vector<int>& nums) {
int k = 0,n = nums.size();

for(int i=0;i<n;i++){
int j = i;
while(j<n and nums[i]==nums[j]){
j++;
}
nums[k++] = nums[i];
i = j -1;
}
return k;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
int reverse(int x) {
long long sum = 0;
while(x != 0){
sum = sum*10 + x%10;
x /= 10;
}
//check whether it is in given range or not
if(sum > -pow(2,31) && sum < pow(2,31)-1){
return sum;
}
return 0;
}
int reverse(int x) {

long long sum = 0;

while(x != 0){

sum = sum*10 + x%10;
x /= 10;

}

//check whether it is in given range or not
if(sum > -pow(2,31) && sum < pow(2,31)-1){
return sum;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int row = matrix.size(), col = matrix[0].size();
bool col0 = 1;
for(int i=0; i<row; i++) {
if(matrix[i][0] == 0)
col0 = 0;
for(int j=1; j<col; j++) {
if(matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int i=row-1; i>=0; i--) {
for(int j=col-1; j>=1; j--) {
if(matrix[i][0] == 0 or matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
if(col0 == 0)
matrix[i][0] = 0;
}
}
};
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {

int row = matrix.size(), col = matrix[0].size();
bool col0 = 1;

for(int i=0; i<row; i++) {
if(matrix[i][0] == 0)
col0 = 0;
for(int j=1; j<col; j++) {
if(matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}

for(int i=row-1; i>=0; i--) {
for(int j=col-1; j>=1; j--) {
if(matrix[i][0] == 0 or matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
if(col0 == 0)
matrix[i][0] = 0;
}

}
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
vector<int> twoSum(vector<int>& A, int target) {
vector<int> ans;
unordered_map<int,int> mymap;
for(int i=0; i<A.size(); i++){
int search = target - A[i];
if(mymap.find(search)==mymap.end()){
mymap[A[i]] = i;
}else{
ans.push_back(mymap[search]);
ans.push_back(i);
return ans;
}
}
return ans;
}
vector<int> twoSum(vector<int>& A, int target) {

vector<int> ans;
unordered_map<int,int> mymap;

for(int i=0; i<A.size(); i++){
int search = target - A[i];

if(mymap.find(search)==mymap.end()){
mymap[A[i]] = i;
}else{
ans.push_back(mymap[search]);
ans.push_back(i);
return ans;
}
}

return ans;

}

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
string longestCommonPrefix(vector<string>& strs) {
//for string the final answer
string ans = "";
if(strs.size() == 0)
return "";
//It will return the minimum element
string s = *min_element(strs.begin(), strs.end());
for(int i=0; i<s.size(); i++){
for(int j=0; j<strs.size(); j++){
if(s[i] != strs[j][i]){
return ans;
}
}
ans.push_back(s[i]);
}
return ans;
string longestCommonPrefix(vector<string>& strs) {

//for string the final answer
string ans = "";

if(strs.size() == 0)
return "";

//It will return the minimum element
string s = *min_element(strs.begin(), strs.end());

for(int i=0; i<s.size(); i++){
for(int j=0; j<strs.size(); j++){
if(s[i] != strs[j][i]){
return ans;
}
}
ans.push_back(s[i]);
}

return ans;
}
Loading