Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d05b0c

Browse files
committed
easy
1 parent a5c8380 commit 8d05b0c

14 files changed

+760
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
7+
};

js/14.最长公共前缀.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @lc app=leetcode.cn id=14 lang=javascript
3+
*
4+
* [14] 最长公共前缀
5+
*
6+
* https://leetcode-cn.com/problems/longest-common-prefix/description/
7+
*
8+
* algorithms
9+
* Easy (34.81%)
10+
* Likes: 704
11+
* Dislikes: 0
12+
* Total Accepted: 124K
13+
* Total Submissions: 356.3K
14+
* Testcase Example: '["flower","flow","flight"]'
15+
*
16+
* 编写一个函数来查找字符串数组中的最长公共前缀。
17+
*
18+
* 如果不存在公共前缀,返回空字符串 ""。
19+
*
20+
* 示例 1:
21+
*
22+
* 输入: ["flower","flow","flight"]
23+
* 输出: "fl"
24+
*
25+
*
26+
* 示例 2:
27+
*
28+
* 输入: ["dog","racecar","car"]
29+
* 输出: ""
30+
* 解释: 输入不存在公共前缀。
31+
*
32+
*
33+
* 说明:
34+
*
35+
* 所有输入只包含小写字母 a-z 。
36+
*
37+
*/
38+
/**
39+
* @param {string[]} strs
40+
* @return {string}
41+
*/
42+
var longestCommonPrefix = function(strs) {
43+
if (!strs || strs.length == 0) {
44+
return "";
45+
}
46+
if (strs.length === 1) {
47+
return strs[0];
48+
}
49+
let r = "";
50+
for (let i = 0; i < strs[0].length; i += 1) {
51+
const c = strs[0][i];
52+
for (let str of strs) {
53+
if (str[i] !== c) {
54+
return r;
55+
}
56+
}
57+
r += c;
58+
}
59+
return r;
60+
};

js/188-best-time-to-buy-and-sell-stock-iv.js

Whitespace-only changes.

js/20.有效的括号.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @lc app=leetcode.cn id=20 lang=javascript
3+
*
4+
* [20] 有效的括号
5+
*
6+
* https://leetcode-cn.com/problems/valid-parentheses/description/
7+
*
8+
* algorithms
9+
* Easy (39.62%)
10+
* Likes: 1069
11+
* Dislikes: 0
12+
* Total Accepted: 125.3K
13+
* Total Submissions: 316.3K
14+
* Testcase Example: '"()"'
15+
*
16+
* 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
17+
*
18+
* 有效字符串需满足:
19+
*
20+
*
21+
* 左括号必须用相同类型的右括号闭合。
22+
* 左括号必须以正确的顺序闭合。
23+
*
24+
*
25+
* 注意空字符串可被认为是有效字符串。
26+
*
27+
* 示例 1:
28+
*
29+
* 输入: "()"
30+
* 输出: true
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
* 输入: "()[]{}"
36+
* 输出: true
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
* 输入: "(]"
42+
* 输出: false
43+
*
44+
*
45+
* 示例 4:
46+
*
47+
* 输入: "([)]"
48+
* 输出: false
49+
*
50+
*
51+
* 示例 5:
52+
*
53+
* 输入: "{[]}"
54+
* 输出: true
55+
*
56+
*/
57+
/**
58+
* @param {string} s
59+
* @return {boolean}
60+
*/
61+
var isValid = function(s) {
62+
if (!s || s.length === 0) {
63+
return true;
64+
}
65+
let stack = [];
66+
for (let c of s) {
67+
if (c === ")") {
68+
if (stack.pop() !== "(") {
69+
return false;
70+
}
71+
continue;
72+
}
73+
if (c === "]") {
74+
if (stack.pop() !== "[") {
75+
return false;
76+
}
77+
continue;
78+
}
79+
if (c === "}") {
80+
if (stack.pop() !== "{") {
81+
return false;
82+
}
83+
continue;
84+
}
85+
stack.push(c);
86+
}
87+
console.log(stack);
88+
return stack.length === 0;
89+
};
90+
91+
isValid("()");

js/21.合并两个有序链表.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode.cn id=21 lang=javascript
3+
*
4+
* [21] 合并两个有序链表
5+
*
6+
* https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
7+
*
8+
* algorithms
9+
* Easy (57.30%)
10+
* Likes: 631
11+
* Dislikes: 0
12+
* Total Accepted: 112.7K
13+
* Total Submissions: 196.7K
14+
* Testcase Example: '[1,2,4]\n[1,3,4]'
15+
*
16+
* 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
17+
*
18+
* 示例:
19+
*
20+
* 输入:1->2->4, 1->3->4
21+
* 输出:1->1->2->3->4->4
22+
*
23+
*
24+
*/
25+
/**
26+
* Definition for singly-linked list.
27+
* function ListNode(val) {
28+
* this.val = val;
29+
* this.next = null;
30+
* }
31+
*/
32+
/**
33+
* @param {ListNode} l1
34+
* @param {ListNode} l2
35+
* @return {ListNode}
36+
*/
37+
var mergeTwoLists = function(l1, l2) {
38+
if (!l1 && !l2) {
39+
console.log("both null");
40+
return l1;
41+
}
42+
let r;
43+
let p;
44+
while (l1 && l2) {
45+
let v1 = l1.val;
46+
let v2 = l2.val;
47+
if (v1 < v2) {
48+
if (!r) {
49+
r = new ListNode(v1);
50+
p = r;
51+
} else {
52+
p.next = new ListNode(v1);
53+
p = p.next;
54+
}
55+
l1 = l1.next;
56+
} else {
57+
if (!r) {
58+
r = new ListNode(v2);
59+
p = r;
60+
} else {
61+
p.next = new ListNode(v2);
62+
p = p.next;
63+
}
64+
l2 = l2.next;
65+
}
66+
}
67+
if (l1) {
68+
if (!r) {
69+
return l1;
70+
}
71+
p.next = l1;
72+
}
73+
if (l2) {
74+
if (!r) {
75+
return l2;
76+
}
77+
p.next = l2;
78+
}
79+
return r;
80+
};

js/27.移除元素.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @lc app=leetcode.cn id=27 lang=javascript
3+
*
4+
* [27] 移除元素
5+
*
6+
* https://leetcode-cn.com/problems/remove-element/description/
7+
*
8+
* algorithms
9+
* Easy (56.07%)
10+
* Likes: 365
11+
* Dislikes: 0
12+
* Total Accepted: 86.6K
13+
* Total Submissions: 154.5K
14+
* Testcase Example: '[3,2,2,3]\n3'
15+
*
16+
* 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。
17+
*
18+
* 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
19+
*
20+
* 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
21+
*
22+
* 示例 1:
23+
*
24+
* 给定 nums = [3,2,2,3], val = 3,
25+
*
26+
* 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。
27+
*
28+
* 你不需要考虑数组中超出新长度后面的元素。
29+
*
30+
*
31+
* 示例 2:
32+
*
33+
* 给定 nums = [0,1,2,2,3,0,4,2], val = 2,
34+
*
35+
* 函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。
36+
*
37+
* 注意这五个元素可为任意顺序。
38+
*
39+
* 你不需要考虑数组中超出新长度后面的元素。
40+
*
41+
*
42+
* 说明:
43+
*
44+
* 为什么返回数值是整数,但输出的答案是数组呢?
45+
*
46+
* 请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
47+
*
48+
* 你可以想象内部操作如下:
49+
*
50+
* // nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
51+
* int len = removeElement(nums, val);
52+
*
53+
* // 在函数里修改输入数组对于调用者是可见的。
54+
* // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
55+
* for (int i = 0; i < len; i++) {
56+
* print(nums[i]);
57+
* }
58+
*
59+
*
60+
*/
61+
/**
62+
* @param {number[]} nums
63+
* @param {number} val
64+
* @return {number}
65+
*/
66+
var removeElement = function(nums, val) {
67+
let result = 0;
68+
const len = nums.length;
69+
for (let i = 0; i < len - result; i++) {
70+
if (nums[i] === val) {
71+
nums[i] = nums[len - result - 1];
72+
result++;
73+
i -= 1; // 重新比较当前数(原数组最后一个)
74+
}
75+
}
76+
console.log(nums.length, result, nums);
77+
return nums.length - result;
78+
};

js/28.实现-str-str.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode.cn id=28 lang=javascript
3+
*
4+
* [28] 实现 strStr()
5+
*
6+
* https://leetcode-cn.com/problems/implement-strstr/description/
7+
*
8+
* algorithms
9+
* Easy (38.90%)
10+
* Likes: 259
11+
* Dislikes: 0
12+
* Total Accepted: 83.1K
13+
* Total Submissions: 213.8K
14+
* Testcase Example: '"hello"\n"ll"'
15+
*
16+
* 实现 strStr() 函数。
17+
*
18+
* 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置
19+
* (从0开始)。如果不存在,则返回  -1。
20+
*
21+
* 示例 1:
22+
*
23+
* 输入: haystack = "hello", needle = "ll"
24+
* 输出: 2
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
* 输入: haystack = "aaaaa", needle = "bba"
30+
* 输出: -1
31+
*
32+
*
33+
* 说明:
34+
*
35+
* 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
36+
*
37+
* 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
38+
*
39+
*/
40+
/**
41+
* @param {string} haystack
42+
* @param {string} needle
43+
* @return {number}
44+
*/
45+
var strStr = function(haystack, needle) {
46+
return haystack.indexOf(needle);
47+
};

0 commit comments

Comments
 (0)