Skip to content

Commit c50ddf5

Browse files
committed
feat: add solutions to lc problems: No.0191,1281
- No.0191.Number of 1 Bits - No.1281.Subtract the Product and Sum of Digits of an Integer
1 parent 36818e7 commit c50ddf5

File tree

8 files changed

+133
-39
lines changed

8 files changed

+133
-39
lines changed

solution/0100-0199/0191.Number of 1 Bits/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,24 @@ impl Solution {
240240
pub fn hammingWeight(mut n: u32) -> i32 {
241241
let mut res = 0;
242242
while n != 0 {
243-
res += n & 1;
244-
n >>= 1;
243+
n &= n - 1;
244+
res += 1;
245245
}
246-
res as i32
246+
res
247247
}
248248
}
249249
```
250250

251-
```rust
252-
impl Solution {
253-
pub fn hammingWeight(mut n: u32) -> i32 {
254-
let mut res = 0;
255-
while n != 0 {
256-
n &= (n - 1);
257-
res += 1;
258-
}
259-
res
251+
### **C**
252+
253+
```c
254+
int hammingWeight(uint32_t n) {
255+
int ans = 0;
256+
while (n) {
257+
n &= n - 1;
258+
ans++;
260259
}
260+
return ans;
261261
}
262262
```
263263

solution/0100-0199/0191.Number of 1 Bits/README_EN.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,24 @@ impl Solution {
190190
pub fn hammingWeight(mut n: u32) -> i32 {
191191
let mut res = 0;
192192
while n != 0 {
193-
res += n & 1;
194-
n >>= 1;
193+
n &= n - 1;
194+
res += 1;
195195
}
196-
res as i32
196+
res
197197
}
198198
}
199199
```
200200

201-
```rust
202-
impl Solution {
203-
pub fn hammingWeight(mut n: u32) -> i32 {
204-
let mut res = 0;
205-
while n != 0 {
206-
n &= (n - 1);
207-
res += 1;
208-
}
209-
res
201+
### **C**
202+
203+
```c
204+
int hammingWeight(uint32_t n) {
205+
int ans = 0;
206+
while (n) {
207+
n &= n - 1;
208+
ans++;
210209
}
210+
return ans;
211211
}
212212
```
213213
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int hammingWeight(uint32_t n) {
2+
int ans = 0;
3+
while (n) {
4+
n &= n - 1;
5+
ans++;
6+
}
7+
return ans;
8+
}

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,53 @@ func subtractProductAndSum(n int) int {
112112
}
113113
```
114114

115+
### **TypeScript**
116+
117+
```ts
118+
function subtractProductAndSum(n: number): number {
119+
let p = 1;
120+
let s = 0;
121+
while (n) {
122+
const num = n % 10;
123+
n = Math.floor(n / 10);
124+
p *= num;
125+
s += num;
126+
}
127+
return p - s;
128+
}
129+
```
130+
115131
### **Rust**
116132

117133
```rust
118134
impl Solution {
119135
pub fn subtract_product_and_sum(mut n: i32) -> i32 {
120-
let mut mul = 1;
121-
let mut sum = 0;
136+
let mut p = 1;
137+
let mut s = 0;
122138
while n != 0 {
123139
let num = n % 10;
124140
n /= 10;
125-
mul *= num;
126-
sum += num;
141+
p *= num;
142+
s += num;
127143
}
128-
mul - sum
144+
p - s
145+
}
146+
}
147+
```
148+
149+
### **C**
150+
151+
```c
152+
int subtractProductAndSum(int n) {
153+
int p = 1;
154+
int s = 0;
155+
while (n) {
156+
int num = n % 10;
157+
n /= 10;
158+
p *= num;
159+
s += num;
129160
}
161+
return p - s;
130162
}
131163
```
132164

solution/1200-1299/1281.Subtract the Product and Sum of Digits of an Integer/README_EN.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,53 @@ func subtractProductAndSum(n int) int {
104104
}
105105
```
106106

107+
### **TypeScript**
108+
109+
```ts
110+
function subtractProductAndSum(n: number): number {
111+
let p = 1;
112+
let s = 0;
113+
while (n) {
114+
const num = n % 10;
115+
n = Math.floor(n / 10);
116+
p *= num;
117+
s += num;
118+
}
119+
return p - s;
120+
}
121+
```
122+
107123
### **Rust**
108124

109125
```rust
110126
impl Solution {
111127
pub fn subtract_product_and_sum(mut n: i32) -> i32 {
112-
let mut mul = 1;
113-
let mut sum = 0;
128+
let mut p = 1;
129+
let mut s = 0;
114130
while n != 0 {
115131
let num = n % 10;
116132
n /= 10;
117-
mul *= num;
118-
sum += num;
133+
p *= num;
134+
s += num;
119135
}
120-
mul - sum
136+
p - s
137+
}
138+
}
139+
```
140+
141+
### **C**
142+
143+
```c
144+
int subtractProductAndSum(int n) {
145+
int p = 1;
146+
int s = 0;
147+
while (n) {
148+
int num = n % 10;
149+
n /= 10;
150+
p *= num;
151+
s += num;
121152
}
153+
return p - s;
122154
}
123155
```
124156
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int subtractProductAndSum(int n) {
2+
int p = 1;
3+
int s = 0;
4+
while (n) {
5+
int num = n % 10;
6+
n /= 10;
7+
p *= num;
8+
s += num;
9+
}
10+
return p - s;
11+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
impl Solution {
22
pub fn subtract_product_and_sum(mut n: i32) -> i32 {
3-
let mut mul = 1;
4-
let mut sum = 0;
3+
let mut p = 1;
4+
let mut s = 0;
55
while n != 0 {
66
let num = n % 10;
77
n /= 10;
8-
mul *= num;
9-
sum += num;
8+
p *= num;
9+
s += num;
1010
}
11-
mul - sum
11+
p - s
1212
}
1313
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function subtractProductAndSum(n: number): number {
2+
let p = 1;
3+
let s = 0;
4+
while (n) {
5+
const num = n % 10;
6+
n = Math.floor(n / 10);
7+
p *= num;
8+
s += num;
9+
}
10+
return p - s;
11+
}

0 commit comments

Comments
 (0)