Skip to content

Commit fbd32eb

Browse files
committed
258. Add Digits
1 parent 4258b39 commit fbd32eb

File tree

310 files changed

+7379
-3580
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

310 files changed

+7379
-3580
lines changed

Diff for: .eslintrc.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true,
5+
node: true
6+
},
7+
extends: [
8+
'eslint:recommended'
9+
],
10+
parserOptions: {
11+
ecmaVersion: 'latest',
12+
sourceType: 'module'
13+
},
14+
rules: {
15+
quotes: ['error', 'single'],
16+
// we want to force semicolons
17+
semi: ['error', 'always'],
18+
// we use 2 spaces to indent our code
19+
indent: ['error', 2]
20+
// we want to avoid extraneous spaces
21+
}
22+
};

Diff for: .gitignore

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/node
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=node
4+
5+
### Node ###
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
.pnpm-debug.log*
14+
15+
# Diagnostic reports (https://nodejs.org/api/report.html)
16+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
17+
18+
# Runtime data
19+
pids
20+
*.pid
21+
*.seed
22+
*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
lib-cov
26+
27+
# Coverage directory used by tools like istanbul
28+
coverage
29+
*.lcov
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (https://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
jspm_packages/
49+
50+
# Snowpack dependency directory (https://snowpack.dev/)
51+
web_modules/
52+
53+
# TypeScript cache
54+
*.tsbuildinfo
55+
56+
# Optional npm cache directory
57+
.npm
58+
59+
# Optional eslint cache
60+
.eslintcache
61+
62+
# Optional stylelint cache
63+
.stylelintcache
64+
65+
# Microbundle cache
66+
.rpt2_cache/
67+
.rts2_cache_cjs/
68+
.rts2_cache_es/
69+
.rts2_cache_umd/
70+
71+
# Optional REPL history
72+
.node_repl_history
73+
74+
# Output of 'npm pack'
75+
*.tgz
76+
77+
# Yarn Integrity file
78+
.yarn-integrity
79+
80+
# dotenv environment variable files
81+
.env
82+
.env.development.local
83+
.env.test.local
84+
.env.production.local
85+
.env.local
86+
87+
# parcel-bundler cache (https://parceljs.org/)
88+
.cache
89+
.parcel-cache
90+
91+
# Next.js build output
92+
.next
93+
out
94+
95+
# Nuxt.js build / generate output
96+
.nuxt
97+
dist
98+
99+
# Gatsby files
100+
.cache/
101+
# Comment in the public line in if your project uses Gatsby and not Next.js
102+
# https://nextjs.org/blog/next-9-1#public-directory-support
103+
# public
104+
105+
# vuepress build output
106+
.vuepress/dist
107+
108+
# vuepress v2.x temp and cache directory
109+
.temp
110+
111+
# Docusaurus cache and generated files
112+
.docusaurus
113+
114+
# Serverless directories
115+
.serverless/
116+
117+
# FuseBox cache
118+
.fusebox/
119+
120+
# DynamoDB Local files
121+
.dynamodb/
122+
123+
# TernJS port file
124+
.tern-port
125+
126+
# Stores VSCode versions used for testing VSCode extensions
127+
.vscode-test
128+
129+
# yarn v2
130+
.yarn/cache
131+
.yarn/unplugged
132+
.yarn/build-state.yml
133+
.yarn/install-state.gz
134+
.pnp.*
135+
136+
### Node Patch ###
137+
# Serverless Webpack directories
138+
.webpack/
139+
140+
# Optional stylelint cache
141+
142+
# SvelteKit build / generate output
143+
.svelte-kit
144+
145+
# End of https://www.toptal.com/developers/gitignore/api/node

Diff for: 1-bitAnd2-bitCharacters.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @return {boolean}
55
*/
66
var isOneBitCharacter = function (bits) {
7-
let ones = 0;
8-
for (i = bits.length - 2; i >= 0 && bits[i] == 1; i--) ones++;
9-
return ones % 2 ? false : true;
7+
let ones = 0;
8+
for (i = bits.length - 2; i >= 0 && bits[i] == 1; i--) ones++;
9+
return ones % 2 ? false : true;
1010
};

Diff for: 4SumII.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* @return {number}
88
*/
99
var fourSumCount = function (A, B, C, D) {
10-
let hashTable = {};
11-
let res = 0;
12-
for (a of A)
13-
for (b of B)
14-
if (hashTable[a + b]) hashTable[a + b] += 1;
15-
else hashTable[a + b] = 1;
16-
for (c of C)
17-
for (d of D)
18-
if (hashTable[-c - d]) res += hashTable[-c - d];
19-
return res;
10+
let hashTable = {};
11+
let res = 0;
12+
for (a of A)
13+
for (b of B)
14+
if (hashTable[a + b]) hashTable[a + b] += 1;
15+
else hashTable[a + b] = 1;
16+
for (c of C)
17+
for (d of D)
18+
if (hashTable[-c - d]) res += hashTable[-c - d];
19+
return res;
2020
};

Diff for: AddBinay.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
* @return {string}
66
*/
77
var addBinary = function (a, b) {
8-
if (b.length > a.length)[a, b] = [b, a];
9-
let add1 = 0;
10-
let aLen = a.length;
11-
a = ("0" + a).split("").reverse().map(function (x) {
12-
return parseInt(x, 2);
13-
});
14-
b = b.split("").reverse().map(function (x) {
15-
return parseInt(x, 2);
16-
});
17-
for (i = 0; i < aLen + 1; i++) {
18-
if (add1 == 1) {
19-
a[i] += 1;
20-
add1 = 0;
21-
}
22-
if (b[i] == null) b.push(0);
23-
a[i] = a[i] + b[i];
24-
if (a[i] >= 2) {
25-
a[i] = a[i] % 2;
26-
add1 = 1;
27-
}
8+
if (b.length > a.length)[a, b] = [b, a];
9+
let add1 = 0;
10+
let aLen = a.length;
11+
a = ('0' + a).split('').reverse().map(function (x) {
12+
return parseInt(x, 2);
13+
});
14+
b = b.split('').reverse().map(function (x) {
15+
return parseInt(x, 2);
16+
});
17+
for (i = 0; i < aLen + 1; i++) {
18+
if (add1 == 1) {
19+
a[i] += 1;
20+
add1 = 0;
2821
}
29-
while (a[a.length - 1] == 0) a.pop();
30-
if (a == "") return "0";
31-
return a.reverse().join("").toString();
22+
if (b[i] == null) b.push(0);
23+
a[i] = a[i] + b[i];
24+
if (a[i] >= 2) {
25+
a[i] = a[i] % 2;
26+
add1 = 1;
27+
}
28+
}
29+
while (a[a.length - 1] == 0) a.pop();
30+
if (a == '') return '0';
31+
return a.reverse().join('').toString();
3232
};

Diff for: AddDigits.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* 258. Add Digits
3+
* @param {number} num
4+
* @return {number}
5+
*/
6+
var addDigits = function(num) {
7+
let sum = num.toString().split('').reduce((sum, x) => Number(sum) + Number(x));
8+
if(sum>9) return addDigits(sum);
9+
return sum;
10+
};

Diff for: AddStrings.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* @return {string}
66
*/
77
var addStrings = function (num1, num2) {
8-
let add1 = 0;
9-
let sum = 0;
10-
let numA = (num1.length >= num2.length ? num1 : num2).split("").reverse();
11-
let numB = (num1.length < num2.length ? num1 : num2).split("").reverse();
12-
numB = numB.concat(Array(numA.length - numB.length).fill("0"))
13-
for (i = 0; i < numB.length; i++) {
14-
sum = parseInt(numA[i], 10) + parseInt(numB[i], 10);
15-
if (sum > 9) add1 = 1;
16-
numA[i] = sum % 10;
17-
if (add1 == 1 && i + 1 == numB.length) numA.push(1);
18-
if (i < numB.length - 1) numA[i + 1] = parseInt(numA[i + 1], 10) + add1;
19-
add1 = 0;
20-
}
21-
return numA.reverse().join("");
8+
let add1 = 0;
9+
let sum = 0;
10+
let numA = (num1.length >= num2.length ? num1 : num2).split('').reverse();
11+
let numB = (num1.length < num2.length ? num1 : num2).split('').reverse();
12+
numB = numB.concat(Array(numA.length - numB.length).fill('0'));
13+
for (i = 0; i < numB.length; i++) {
14+
sum = parseInt(numA[i], 10) + parseInt(numB[i], 10);
15+
if (sum > 9) add1 = 1;
16+
numA[i] = sum % 10;
17+
if (add1 == 1 && i + 1 == numB.length) numA.push(1);
18+
if (i < numB.length - 1) numA[i + 1] = parseInt(numA[i + 1], 10) + add1;
19+
add1 = 0;
20+
}
21+
return numA.reverse().join('');
2222
};

Diff for: AddToArray-FormOfInteger.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* @return {number[]}
66
*/
77
var addToArrayForm = function (A, K) {
8-
let res = [];
9-
let add = 0;
10-
let carry = 0;
11-
K = K.toString(10).split("")
12-
while (A.length != 0 || K.length != 0) {
13-
let aNum = A.length != 0 ? parseInt(A.pop(), 10) : 0;
14-
let kNum = K.length != 0 ? parseInt(K.pop(), 10) : 0;
15-
add = aNum + kNum + carry;
16-
carry = Math.floor(add / 10);
17-
add = add % 10;
18-
res.unshift(add);
19-
}
20-
if (carry == 1) res.unshift(1);
21-
return res;
8+
let res = [];
9+
let add = 0;
10+
let carry = 0;
11+
K = K.toString(10).split('');
12+
while (A.length != 0 || K.length != 0) {
13+
let aNum = A.length != 0 ? parseInt(A.pop(), 10) : 0;
14+
let kNum = K.length != 0 ? parseInt(K.pop(), 10) : 0;
15+
add = aNum + kNum + carry;
16+
carry = Math.floor(add / 10);
17+
add = add % 10;
18+
res.unshift(add);
19+
}
20+
if (carry == 1) res.unshift(1);
21+
return res;
2222
};

0 commit comments

Comments
 (0)