Skip to content

Commit 8fabb29

Browse files
authored
feat: add solutions to lc problems: No.1015,1017,1073,2109 (#910)
* No.1015.Smallest Integer Divisible by K * No.1017.Convert to Base -2 * No.1073.Adding Two Negabinary Numbers * No.2109.Adding Spaces to a String
1 parent 05bf3a1 commit 8fabb29

File tree

12 files changed

+219
-2
lines changed

12 files changed

+219
-2
lines changed

solution/1000-1099/1015.Smallest Integer Divisible by K/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ func smallestRepunitDivByK(k int) int {
124124
}
125125
```
126126

127+
### **TypeScript**
128+
129+
```ts
130+
function smallestRepunitDivByK(k: number): number {
131+
let n = 1 % k;
132+
for (let i = 1; i <= k; ++i) {
133+
if (n === 0) {
134+
return i;
135+
}
136+
n = (n * 10 + 1) % k;
137+
}
138+
return -1;
139+
}
140+
```
141+
127142
### **...**
128143

129144
```

solution/1000-1099/1015.Smallest Integer Divisible by K/README_EN.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ func smallestRepunitDivByK(k int) int {
109109
}
110110
```
111111

112+
### **TypeScript**
113+
114+
```ts
115+
function smallestRepunitDivByK(k: number): number {
116+
let n = 1 % k;
117+
for (let i = 1; i <= k; ++i) {
118+
if (n === 0) {
119+
return i;
120+
}
121+
n = (n * 10 + 1) % k;
122+
}
123+
return -1;
124+
}
125+
```
126+
112127
### **...**
113128

114129
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function smallestRepunitDivByK(k: number): number {
2+
let n = 1 % k;
3+
for (let i = 1; i <= k; ++i) {
4+
if (n === 0) {
5+
return i;
6+
}
7+
n = (n * 10 + 1) % k;
8+
}
9+
return -1;
10+
}

solution/1000-1099/1017.Convert to Base -2/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,29 @@ func baseNeg2(n int) string {
162162
}
163163
```
164164

165+
### **TypeScript**
166+
167+
```ts
168+
function baseNeg2(n: number): string {
169+
if (n === 0) {
170+
return '0';
171+
}
172+
let k = 1;
173+
let ans: string[] = [];
174+
while (n) {
175+
if (n % 2) {
176+
ans.push('1');
177+
n -= k;
178+
} else {
179+
ans.push('0');
180+
}
181+
k *= -1;
182+
n /= 2;
183+
}
184+
return ans.reverse().join('');
185+
}
186+
```
187+
165188
### **...**
166189

167190
```

solution/1000-1099/1017.Convert to Base -2/README_EN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,29 @@ func baseNeg2(n int) string {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function baseNeg2(n: number): string {
147+
if (n === 0) {
148+
return '0';
149+
}
150+
let k = 1;
151+
let ans: string[] = [];
152+
while (n) {
153+
if (n % 2) {
154+
ans.push('1');
155+
n -= k;
156+
} else {
157+
ans.push('0');
158+
}
159+
k *= -1;
160+
n /= 2;
161+
}
162+
return ans.reverse().join('');
163+
}
164+
```
165+
143166
### **...**
144167

145168
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function baseNeg2(n: number): string {
2+
if (n === 0) {
3+
return '0';
4+
}
5+
let k = 1;
6+
let ans: string[] = [];
7+
while (n) {
8+
if (n % 2) {
9+
ans.push('1');
10+
n -= k;
11+
} else {
12+
ans.push('0');
13+
}
14+
k *= -1;
15+
n /= 2;
16+
}
17+
return ans.reverse().join('');
18+
}

solution/1000-1099/1073.Adding Two Negabinary Numbers/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,35 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
194194
}
195195
```
196196

197+
### **TypeScript**
198+
199+
```ts
200+
function addNegabinary(arr1: number[], arr2: number[]): number[] {
201+
let i = arr1.length - 1,
202+
j = arr2.length - 1;
203+
const ans: number[] = [];
204+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
205+
const a = i < 0 ? 0 : arr1[i];
206+
const b = j < 0 ? 0 : arr2[j];
207+
let x = a + b + c;
208+
c = 0;
209+
if (x > 1) {
210+
x -= 2;
211+
c -= 1;
212+
}
213+
if (x < 0) {
214+
x += 2;
215+
c += 1;
216+
}
217+
ans.push(x);
218+
}
219+
while (ans.length > 1 && ans[ans.length - 1] === 0) {
220+
ans.pop();
221+
}
222+
return ans.reverse();
223+
}
224+
```
225+
197226
### **...**
198227

199228
```

solution/1000-1099/1073.Adding Two Negabinary Numbers/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,35 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
169169
}
170170
```
171171

172+
### **TypeScript**
173+
174+
```ts
175+
function addNegabinary(arr1: number[], arr2: number[]): number[] {
176+
let i = arr1.length - 1,
177+
j = arr2.length - 1;
178+
const ans: number[] = [];
179+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
180+
const a = i < 0 ? 0 : arr1[i];
181+
const b = j < 0 ? 0 : arr2[j];
182+
let x = a + b + c;
183+
c = 0;
184+
if (x > 1) {
185+
x -= 2;
186+
c -= 1;
187+
}
188+
if (x < 0) {
189+
x += 2;
190+
c += 1;
191+
}
192+
ans.push(x);
193+
}
194+
while (ans.length > 1 && ans[ans.length - 1] === 0) {
195+
ans.pop();
196+
}
197+
return ans.reverse();
198+
}
199+
```
200+
172201
### **...**
173202

174203
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function addNegabinary(arr1: number[], arr2: number[]): number[] {
2+
let i = arr1.length - 1,
3+
j = arr2.length - 1;
4+
const ans: number[] = [];
5+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
6+
const a = i < 0 ? 0 : arr1[i];
7+
const b = j < 0 ? 0 : arr2[j];
8+
let x = a + b + c;
9+
c = 0;
10+
if (x > 1) {
11+
x -= 2;
12+
c -= 1;
13+
}
14+
if (x < 0) {
15+
x += 2;
16+
c += 1;
17+
}
18+
ans.push(x);
19+
}
20+
while (ans.length > 1 && ans[ans.length - 1] === 0) {
21+
ans.pop();
22+
}
23+
return ans.reverse();
24+
}

solution/2100-2199/2109.Adding Spaces to a String/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,17 @@ func addSpaces(s string, spaces []int) string {
162162
<!-- 这里可写当前语言的特殊实现逻辑 -->
163163

164164
```ts
165-
165+
function addSpaces(s: string, spaces: number[]): string {
166+
let ans = '';
167+
for (let i = 0, j = 0; i < s.length; i++) {
168+
if (j < spaces.length && i === spaces[j]) {
169+
ans += ' ';
170+
++j;
171+
}
172+
ans += s[i];
173+
}
174+
return ans;
175+
}
166176
```
167177

168178
### **...**

0 commit comments

Comments
 (0)