Skip to content

Commit 5f4be04

Browse files
committed
78
1 parent 242a356 commit 5f4be04

9 files changed

+176
-0
lines changed

78.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
void solve(vector<int>&nums,vector<int>&temp,vector<vector<int>>&A,int ind){
2+
if(ind > nums.size()){
3+
return ;
4+
}
5+
A.push_back(temp);
6+
7+
for(int i=ind;i<nums.size();i++){
8+
temp.push_back(nums[i]);
9+
solve(nums,temp,A,i+1);
10+
temp.pop_back();
11+
}
12+
13+
return ;
14+
}
15+
class Solution {
16+
public:
17+
vector<vector<int>> subsets(vector<int>& nums) {
18+
vector<vector<int>>A;
19+
vector<int>temp;
20+
solve(nums,temp,A,0);
21+
return A;
22+
}
23+
};

New Text Document (2).txt

Whitespace-only changes.

New Text Document.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int firstUniqChar(string s) {
4+
vector<int>A(26,0);
5+
for(int i=0;i<s.length();i++){
6+
A[s[i]-'a']++;
7+
}
8+
9+
for(int j=0;j<s.length();j++){
10+
if(A[s[j]-'a'] == 1){
11+
return j;
12+
}
13+
}
14+
15+
return -1;
16+
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int singleNumber(vector<int>& nums) {
4+
5+
long long int result=0;
6+
int n=nums.size();
7+
int bits[32]= {0};
8+
9+
for(int i=0;i<32;i++){
10+
for(int n:nums){
11+
12+
bits[i]+= (n>>i) & 1;
13+
bits[i]=bits[i]%3;
14+
15+
16+
}
17+
}
18+
19+
for(int i=0;i<32;i++){
20+
result=result+pow(2,i)*bits[i];
21+
// cout<<result<<endl;
22+
}
23+
24+
return result;
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int numTrees(int n) {
4+
5+
int dp[n+1];
6+
dp[0]=dp[1]=1;
7+
for(int i=2;i<=n;i++){
8+
dp[i]=0;
9+
for(int j=1;j<=i;j++){
10+
dp[i]+=dp[j-1]*dp[i-j];
11+
}
12+
}
13+
14+
return dp[n];
15+
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int findDuplicate(vector<int>& nums) {
4+
int slow=nums[0];
5+
int fast=nums[0];
6+
7+
do{
8+
slow=nums[slow];
9+
fast=nums[nums[fast]];
10+
}
11+
while(slow!=fast);
12+
13+
slow=nums[0];
14+
while(slow!=fast){
15+
fast=nums[fast];
16+
slow=nums[slow];
17+
}
18+
return fast;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
void sumtotal(TreeNode * root , int value ,int & sum){
2+
3+
if(root->left==NULL && root->right==NULL){
4+
sum+=(value*10)+root->val;
5+
return ;
6+
}
7+
value=value*10 + root->val;
8+
if(root->left){
9+
sumtotal(root->left,value,sum);
10+
}
11+
if(root->right){
12+
sumtotal(root->right,value,sum);
13+
}
14+
return ;
15+
}
16+
17+
class Solution {
18+
public:
19+
int sumNumbers(TreeNode* root) {
20+
if(root==NULL){
21+
return 0;
22+
}
23+
int value=0;
24+
int sum=0;
25+
26+
sumtotal(root,value,sum);
27+
return sum;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int numSquares(int n) {
4+
int dp[n+1];
5+
dp[0]=0;
6+
dp[1]=1;
7+
for(int i=2;i<=n;i++){
8+
dp[i]=INT_MAX;
9+
}
10+
for(int i=2;i<=n;i++){
11+
for(int j=1;j*j<=i;j++){
12+
dp[i]= min(dp[i], dp[i-(j*j)] + 1) ;
13+
}
14+
}
15+
return dp[n];
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
class Solution {
3+
public:
4+
int countNodes(TreeNode* root) {
5+
if(root==NULL){
6+
return 0;
7+
}
8+
int left_height=1;
9+
TreeNode * left= root->left;
10+
while(left!=NULL){
11+
left=left->left;
12+
left_height++;
13+
}
14+
int right_height=1;
15+
TreeNode * right= root->right;
16+
while(right!=NULL){
17+
right=right->right;
18+
right_height++;
19+
}
20+
if(right_height==left_height){
21+
return pow(2,right_height) -1 ;
22+
}
23+
24+
return 1 + countNodes(root->left) + countNodes(root->right);
25+
}
26+
};

0 commit comments

Comments
 (0)