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 6994c5e

Browse files
add: 516 & 523
1 parent 2add847 commit 6994c5e

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
5353
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [JavaScript](./src/shuffle-an-array/res.js)|Medium|
5454
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [JavaScript](./src/sum-of-left-leaves/res.js)|Easy|
5555
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/) | [JavaScript](./src/number-of-segments-in-a-string/res.js)|Easy|
56+
|516|[Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [JavaScript](./src/longest-palindromic-subsequence/res.js)|Medium|
57+
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [JavaScript](./src/continuous-subarray-sum/res.js)|Medium|
5658
||[]() | [](./src//res.js)||

‎src/continuous-subarray-sum/res.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017年04月06日 14:45:14
5+
*
6+
* Problem: Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.
7+
*
8+
* Reference: https://discuss.leetcode.com/topic/80793/java-o-n-time-o-k-space
9+
* Idea: We iterate through the input array exactly once, keeping track of the running sum mod k of the elements in the process. If we find that a running sum value at index j has been previously seen before in some earlier index i in the array, then we know that the sub-array (i,j] contains a desired sum.
10+
*
11+
* @param {number[]} nums
12+
* @param {number} k
13+
* @return {boolean}
14+
*/
15+
let checkSubarraySum = function(nums, k) {
16+
let map = new Map(),
17+
len = nums.length,
18+
sum = 0;
19+
20+
if (len < 2) return false;
21+
map.set(0, -1);
22+
23+
for (let i = 0; i < len; i++) {
24+
sum += nums[i];
25+
if (k !== 0) {
26+
sum %= k;
27+
}
28+
29+
let preInd = map.get(sum);
30+
// console.log('i', i, 'preInd', preInd);
31+
if (preInd !== undefined) {
32+
if (i - preInd > 1) return true;
33+
} else {
34+
map.set(sum, i);
35+
}
36+
}
37+
38+
return false;
39+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017年04月06日 16:14:56
5+
*
6+
* Problem: Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
7+
*
8+
* Tips: What you should pay attention is that, the subsequence is not guaranteed to be continuous, which means some characters would be deleted when it meets the maximum situation. It confused me for quite a long time.
9+
*
10+
* @param {string} s
11+
* @return {number}
12+
*/
13+
let longestPalindromeSubseq = function(s) {
14+
let len = s.length,
15+
matrix = new Array(len);
16+
17+
for (let i = 0; i < len; i++) {
18+
matrix[i] = new Array(len);
19+
matrix[i][i] = 1;
20+
}
21+
22+
for (let i = len - 1; i >= 0; i--) {
23+
for (let j = i + 1; j < len; j++) {
24+
if (s[i] === s[j]) {
25+
matrix[i][j] = j - i === 1 ? 2 : matrix[i + 1][j - 1] + 2;
26+
} else {
27+
matrix[i][j] = max(matrix[i][j - 1], matrix[i + 1][j]);
28+
}
29+
}
30+
}
31+
32+
return matrix[0][len - 1];
33+
};
34+
35+
let max = function(a, b) {
36+
if (a > b) return a;
37+
else return b;
38+
}

0 commit comments

Comments
(0)

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