Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcof2 problems #629

Merged
merged 3 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: update README.md
  • Loading branch information
Yousazoe committed Dec 12, 2021
commit ad1c43311e303cdcf939b6a0adfafa73e370f188
30 changes: 30 additions & 0 deletions solution/0000-0099/0067.Add Binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
string addBinary(string a, string b) {
string res;
int carry = 0;

int i = a.size() - 1;
int j = b.size() - 1;

while (i >= 0 || j >= 0) {
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
int sum = digitA + digitB + carry;
carry = sum >= 2 ? 1 : 0;
sum = sum >= 2 ? sum - 2 : sum;
res += to_string(sum);
}

if (carry == 1) res.push_back('1');
reverse(res.begin(), res.end());
return res;
}
};
```



### **C#**

```cs
Expand Down
29 changes: 29 additions & 0 deletions solution/0000-0099/0067.Add Binary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
string addBinary(string a, string b) {
string res;
int carry = 0;

int i = a.size() - 1;
int j = b.size() - 1;

while (i >= 0 || j >= 0) {
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
int sum = digitA + digitB + carry;
carry = sum >= 2 ? 1 : 0;
sum = sum >= 2 ? sum - 2 : sum;
res += to_string(sum);
}

if (carry == 1) res.push_back('1');
reverse(res.begin(), res.end());
return res;
}
};
```


### **C#**

```cs
Expand Down
23 changes: 23 additions & 0 deletions solution/0000-0099/0067.Add Binary/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
string addBinary(string a, string b) {
string res;
int carry = 0;

int i = a.size() - 1;
int j = b.size() - 1;

while (i >= 0 || j >= 0) {
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
int sum = digitA + digitB + carry;
carry = sum >= 2 ? 1 : 0;
sum = sum >= 2 ? sum - 2 : sum;
res += to_string(sum);
}

if (carry == 1) res.push_back('1');
reverse(res.begin(), res.end());
return res;
}
};
16 changes: 16 additions & 0 deletions solution/0300-0399/0338.Counting Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@

```

### **C++**

```cpp
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
for (int i = 1; i <= n; i++) {
res[i] = res[i & (i - 1)] + 1;
}

return res;
}
};
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0300-0399/0338.Counting Bits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@

```

### **C++**

```cpp
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
for (int i = 1; i <= n; i++) {
res[i] = res[i & (i - 1)] + 1;
}

return res;
}
};
```

### **...**

```
Expand Down
11 changes: 11 additions & 0 deletions solution/0300-0399/0338.Counting Bits/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1);
for (int i = 1; i <= n; i++) {
res[i] = res[i & (i - 1)] + 1;
}

return res;
}
};