Skip to content

Commit 7c4cc8d

Browse files
committed
feat: add solutions of Two Sum(001) and Self Dividing Numbers(728) with JavaScript by Taryn.
1 parent a93f230 commit 7c4cc8d

File tree

5 files changed

+77
-8
lines changed

5 files changed

+77
-8
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@
138138
## 贡献者名单
139139
排名不分先后
140140
- 感谢[@Mukyu](https://github.com/Mukyu), 提供**Java**题解。
141+
- 感谢[@taryn](https://github.com/taryn2016), 提供**JavaScript**题解。
141142
- 感谢[@Wang-YS](https://github.com/Wang-YS), 提供**JavaScrip**代码。
142-
- 感谢[@Blankj](https://github.com/Blankj), 提供**Java**题解、**镇楼诗**及文档编写规范
143-
- 感谢[@WangXin](https://github.com/relish-wang), 提供**kotlin**代码、文档整理及库维护规范
143+
- 感谢[@Blankj](https://github.com/Blankj), 提供**Java**题解、**镇楼诗****文档编写规范**
144+
- 感谢[@WangXin](https://github.com/relish-wang), 提供**kotlin**代码、**文档整理****库维护规范**
144145

145146
[companies]: ./Companies.md
146147

src/_001/Solution1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
for (let i = 0; i < nums.length - 1; i++) {
8+
res = target - nums[i]
9+
for (let j = i + 1; j < nums.length; j++) {
10+
if (nums[j] === res) {
11+
return [i, j]
12+
}
13+
}
14+
if (i === nums.length) {
15+
return []
16+
}
17+
}
18+
}

src/_728/Solution1.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number} left
3+
* @param {number} right
4+
* @return {number[]}
5+
*/
6+
var selfDividingNumbers = function(left, right) {
7+
var result = []
8+
for (var i = left; i <= right; i++ ) {
9+
if (i < 10) {
10+
result.push(i)
11+
} else {
12+
if (/0/.test(i.toString())) {
13+
i = i + 1
14+
}
15+
for (var j = 0; j < i.toString().length; j++) {
16+
if (i % Number(i.toString().split('')[j]) !== 0) {
17+
break
18+
}
19+
if (i % Number(i.toString().split('')[j]) === 0 && j === i.toString().length - 1) {
20+
console.log(i);
21+
result.push(i)
22+
}
23+
}
24+
}
25+
}
26+
return result
27+
};

tips/001/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ return [0, 1].
2121
## 思路 0
2222

2323
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为 `O(n^2)`,首次提交居然是 2ms,打败了 100% 的提交,谜一样的结果,之后再次提交就再也没跑到过 2ms 了。
24-
24+
java:
2525
```java
2626
class Solution {
2727
public int[] twoSum(int[] nums, int target) {
@@ -36,7 +36,20 @@ class Solution {
3636
}
3737
}
3838
```
39-
39+
javascript
40+
```javascript
41+
var twoSum = function(nums, target) {
42+
for (let i = 0; i < nums.length - 1; i++) {
43+
res = target - nums[i]
44+
for (let j = i + 1; j < nums.length; j++) {
45+
if (nums[j] === res) {
46+
return [i, j]
47+
}
48+
}
49+
}
50+
}
51+
```
52+
kotlin:
4053
```kotlin
4154
class Solution {
4255
fun twoSum(nums: IntArray, target: Int): IntArray {

tips/728/README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[Self Dividing Numbers][title]
2+
13
## Description
24

35
A self-dividing number is a number that is divisible by every digit it contains.
@@ -20,12 +22,13 @@ Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
2022

2123
The boundaries of each input argument are 1 <= left <= right <= 10000.
2224

23-
## 思路
25+
**Tags:** Math
2426

25-
算出所给范围内的符合要求(自身能被所有位数上的数字所整除,例如128能被1、2、8整除,且自身不能含有0)的数字。范围为1-10000,范围较小可先打表。
27+
## 思路 0
2628

27-
### 打表(Java)
29+
算出所给范围内的符合要求(自身能被所有位数上的数字所整除,例如128能被1、2、8整除,且自身不能含有0)的数字。范围为1-10000,范围较小可先打表。
2830

31+
java:
2932
```Java
3033
int sum = 0;
3134
for (int i = left; i < right; i++) {
@@ -48,7 +51,8 @@ for (int i = left; i < right; i++) {
4851
}
4952
```
5053

51-
### JavaScript
54+
## 思路 1
55+
javascript:
5256
```JavaScript
5357
var selfDividingNumbers = function(left, right) {
5458
var result = []
@@ -73,3 +77,9 @@ var selfDividingNumbers = function(left, right) {
7377
return result
7478
};
7579
```
80+
## 结语
81+
82+
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
83+
84+
[title]: https://leetcode.com/problems/self-dividing-numbers/description/
85+
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)