-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution for find missing and repeated values.
- Loading branch information
1 parent
a3fac8b
commit 09fcde1
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) { | ||
int n = grid.size()*grid.size(); | ||
int *nums = new int[n]; | ||
for(int i=0; i<n; i++) nums[i] = -1; | ||
vector<int> res = {-1, -1}; | ||
|
||
for(int i=0; i<grid.size(); i++){ | ||
for(int j=0; j<grid[i].size(); j++){ | ||
if(nums[grid[i][j]-1] != 1) | ||
nums[grid[i][j]-1] = 1; | ||
else | ||
nums[grid[i][j]-1] = 0; | ||
} | ||
} | ||
|
||
for(int i=0; i<n; i++){ | ||
if(nums[i] == 0) res[0] = (i+1); | ||
else if(nums[i] == -1) res[1] = (i+1); | ||
} | ||
if(res[0] == -1) res[0] = n; | ||
delete[] nums; | ||
return res; | ||
} | ||
}; |