Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b7f060

Browse files
authored
feat: add js solution to lc problem: No.0726 (doocs#3322)
1 parent cf59818 commit 1b7f060

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

‎solution/0700-0799/0726.Number of Atoms/README.md‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,146 @@ class Solution {
155155

156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function countOfAtoms(formula: string): string {
162+
const getCount = (formula: string, factor = 1) => {
163+
const n = formula.length;
164+
const cnt: Record<string, number> = {};
165+
const s: string[] = [];
166+
let [atom, c] = ['', 0];
167+
168+
for (let i = 0; i <= n; i++) {
169+
if (formula[i] === '(') {
170+
const stk: string[] = ['('];
171+
let j = i;
172+
while (stk.length) {
173+
j++;
174+
if (formula[j] === '(') stk.push('(');
175+
else if (formula[j] === ')') stk.pop();
176+
}
177+
178+
const molecule = formula.slice(i + 1, j);
179+
const nextFactor: string[] = [];
180+
181+
while (isDigit(formula[++j])) {
182+
nextFactor.push(formula[j]);
183+
}
184+
185+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
186+
for (const [atom, c] of Object.entries(nextC)) {
187+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
188+
}
189+
190+
i = j - 1;
191+
continue;
192+
}
193+
194+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
195+
[atom, c] = getAtom(s);
196+
197+
c *= factor;
198+
cnt[atom] = (cnt[atom] ?? 0) + c;
199+
s.length = 0;
200+
}
201+
202+
s.push(formula[i]);
203+
}
204+
205+
return cnt;
206+
};
207+
208+
return Object.entries(getCount(formula))
209+
.sort(([a], [b]) => a.localeCompare(b))
210+
.map(([a, b]) => (b > 1 ? a + b : a))
211+
.join('');
212+
}
213+
214+
const regex = {
215+
atom: /(\D+)(\d+)?/,
216+
isUpper: /[A-Z]+/,
217+
};
218+
const getAtom = (s: string[]): [string, number] => {
219+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
220+
return [atom, c ? +c : 1];
221+
};
222+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
223+
const isUpper = (ch: string) => regex.isUpper.test(ch);
224+
```
225+
226+
#### JavaScript
227+
228+
```js
229+
/**
230+
* @param {string} formula
231+
* @return {string}
232+
*/
233+
var countOfAtoms = function (formula) {
234+
const getCount = (formula, factor = 1) => {
235+
const n = formula.length;
236+
const cnt = {};
237+
const s = [];
238+
let [atom, c] = ['', 0];
239+
240+
for (let i = 0; i <= n; i++) {
241+
if (formula[i] === '(') {
242+
const stk = ['('];
243+
let j = i;
244+
while (stk.length) {
245+
j++;
246+
if (formula[j] === '(') stk.push('(');
247+
else if (formula[j] === ')') stk.pop();
248+
}
249+
250+
const molecule = formula.slice(i + 1, j);
251+
const nextFactor = [];
252+
253+
while (isDigit(formula[++j])) {
254+
nextFactor.push(formula[j]);
255+
}
256+
257+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
258+
for (const [atom, c] of Object.entries(nextC)) {
259+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
260+
}
261+
262+
i = j - 1;
263+
continue;
264+
}
265+
266+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
267+
[atom, c] = getAtom(s);
268+
269+
c *= factor;
270+
cnt[atom] = (cnt[atom] ?? 0) + c;
271+
s.length = 0;
272+
}
273+
274+
s.push(formula[i]);
275+
}
276+
277+
return cnt;
278+
};
279+
280+
return Object.entries(getCount(formula))
281+
.sort(([a], [b]) => a.localeCompare(b))
282+
.map(([a, b]) => (b > 1 ? a + b : a))
283+
.join('');
284+
};
285+
286+
const regex = {
287+
atom: /(\D+)(\d+)?/,
288+
isUpper: /[A-Z]+/,
289+
};
290+
const getAtom = s => {
291+
const [_, atom, c] = regex.atom.exec(s.join(''));
292+
return [atom, c ? +c : 1];
293+
};
294+
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
295+
const isUpper = ch => regex.isUpper.test(ch);
296+
```
297+
158298
<!-- tabs:end -->
159299
160300
<!-- solution:end -->

‎solution/0700-0799/0726.Number of Atoms/README_EN.md‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,146 @@ class Solution {
155155

156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function countOfAtoms(formula: string): string {
162+
const getCount = (formula: string, factor = 1) => {
163+
const n = formula.length;
164+
const cnt: Record<string, number> = {};
165+
const s: string[] = [];
166+
let [atom, c] = ['', 0];
167+
168+
for (let i = 0; i <= n; i++) {
169+
if (formula[i] === '(') {
170+
const stk: string[] = ['('];
171+
let j = i;
172+
while (stk.length) {
173+
j++;
174+
if (formula[j] === '(') stk.push('(');
175+
else if (formula[j] === ')') stk.pop();
176+
}
177+
178+
const molecule = formula.slice(i + 1, j);
179+
const nextFactor: string[] = [];
180+
181+
while (isDigit(formula[++j])) {
182+
nextFactor.push(formula[j]);
183+
}
184+
185+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
186+
for (const [atom, c] of Object.entries(nextC)) {
187+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
188+
}
189+
190+
i = j - 1;
191+
continue;
192+
}
193+
194+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
195+
[atom, c] = getAtom(s);
196+
197+
c *= factor;
198+
cnt[atom] = (cnt[atom] ?? 0) + c;
199+
s.length = 0;
200+
}
201+
202+
s.push(formula[i]);
203+
}
204+
205+
return cnt;
206+
};
207+
208+
return Object.entries(getCount(formula))
209+
.sort(([a], [b]) => a.localeCompare(b))
210+
.map(([a, b]) => (b > 1 ? a + b : a))
211+
.join('');
212+
}
213+
214+
const regex = {
215+
atom: /(\D+)(\d+)?/,
216+
isUpper: /[A-Z]+/,
217+
};
218+
const getAtom = (s: string[]): [string, number] => {
219+
const [_, atom, c] = regex.atom.exec(s.join(''))!;
220+
return [atom, c ? +c : 1];
221+
};
222+
const isDigit = (ch: string) => !Number.isNaN(Number.parseInt(ch));
223+
const isUpper = (ch: string) => regex.isUpper.test(ch);
224+
```
225+
226+
#### JavaScript
227+
228+
```js
229+
/**
230+
* @param {string} formula
231+
* @return {string}
232+
*/
233+
var countOfAtoms = function (formula) {
234+
const getCount = (formula, factor = 1) => {
235+
const n = formula.length;
236+
const cnt = {};
237+
const s = [];
238+
let [atom, c] = ['', 0];
239+
240+
for (let i = 0; i <= n; i++) {
241+
if (formula[i] === '(') {
242+
const stk = ['('];
243+
let j = i;
244+
while (stk.length) {
245+
j++;
246+
if (formula[j] === '(') stk.push('(');
247+
else if (formula[j] === ')') stk.pop();
248+
}
249+
250+
const molecule = formula.slice(i + 1, j);
251+
const nextFactor = [];
252+
253+
while (isDigit(formula[++j])) {
254+
nextFactor.push(formula[j]);
255+
}
256+
257+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
258+
for (const [atom, c] of Object.entries(nextC)) {
259+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
260+
}
261+
262+
i = j - 1;
263+
continue;
264+
}
265+
266+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
267+
[atom, c] = getAtom(s);
268+
269+
c *= factor;
270+
cnt[atom] = (cnt[atom] ?? 0) + c;
271+
s.length = 0;
272+
}
273+
274+
s.push(formula[i]);
275+
}
276+
277+
return cnt;
278+
};
279+
280+
return Object.entries(getCount(formula))
281+
.sort(([a], [b]) => a.localeCompare(b))
282+
.map(([a, b]) => (b > 1 ? a + b : a))
283+
.join('');
284+
};
285+
286+
const regex = {
287+
atom: /(\D+)(\d+)?/,
288+
isUpper: /[A-Z]+/,
289+
};
290+
const getAtom = s => {
291+
const [_, atom, c] = regex.atom.exec(s.join(''));
292+
return [atom, c ? +c : 1];
293+
};
294+
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
295+
const isUpper = ch => regex.isUpper.test(ch);
296+
```
297+
158298
<!-- tabs:end -->
159299
160300
<!-- solution:end -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @param {string} formula
3+
* @return {string}
4+
*/
5+
var countOfAtoms = function (formula) {
6+
const getCount = (formula, factor = 1) => {
7+
const n = formula.length;
8+
const cnt = {};
9+
const s = [];
10+
let [atom, c] = ['', 0];
11+
12+
for (let i = 0; i <= n; i++) {
13+
if (formula[i] === '(') {
14+
const stk = ['('];
15+
let j = i;
16+
while (stk.length) {
17+
j++;
18+
if (formula[j] === '(') stk.push('(');
19+
else if (formula[j] === ')') stk.pop();
20+
}
21+
22+
const molecule = formula.slice(i + 1, j);
23+
const nextFactor = [];
24+
25+
while (isDigit(formula[++j])) {
26+
nextFactor.push(formula[j]);
27+
}
28+
29+
const nextC = getCount(molecule, +nextFactor.join('') || 1);
30+
for (const [atom, c] of Object.entries(nextC)) {
31+
cnt[atom] = (cnt[atom] ?? 0) + c * factor;
32+
}
33+
34+
i = j - 1;
35+
continue;
36+
}
37+
38+
if (s.length && (!formula[i] || isUpper(formula[i]))) {
39+
[atom, c] = getAtom(s);
40+
41+
c *= factor;
42+
cnt[atom] = (cnt[atom] ?? 0) + c;
43+
s.length = 0;
44+
}
45+
46+
s.push(formula[i]);
47+
}
48+
49+
return cnt;
50+
};
51+
52+
return Object.entries(getCount(formula))
53+
.sort(([a], [b]) => a.localeCompare(b))
54+
.map(([a, b]) => (b > 1 ? a + b : a))
55+
.join('');
56+
};
57+
58+
const regex = {
59+
atom: /(\D+)(\d+)?/,
60+
isUpper: /[A-Z]+/,
61+
};
62+
const getAtom = s => {
63+
const [_, atom, c] = regex.atom.exec(s.join(''));
64+
return [atom, c ? +c : 1];
65+
};
66+
const isDigit = ch => !Number.isNaN(Number.parseInt(ch));
67+
const isUpper = ch => regex.isUpper.test(ch);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /