Movatterモバイル変換


[0]ホーム

URL:


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

Commit3b706d4

Browse files
committed
add some more questions and memo
1 parent1b192f7 commit3b706d4

11 files changed

+375
-149
lines changed

‎207 Course Schedule.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,25 @@
88
*@return {boolean}
99
*/
1010

11+
// http://www.cnblogs.com/Liok3187/p/4752700.html
12+
1113
varconstructGraph=function(numNodes,pre){
1214
varnodes=[];
1315
for(vari=0;i<numNodes;i++){
1416
varnode={};
1517
node.neighbors=[];
16-
nodes[i]=node;
18+
nodes.push(node);
1719
}
1820
for(varj=0;j<pre.length;j++){
19-
vars=pre[j][1];
20-
vard=pre[j][0];
21-
nodes[s].neighbors.push(nodes[d]);
21+
varrequiredCourse=pre[j][1];
22+
varcourse=pre[j][0];
23+
// pushing course that require required-course to it's neighbor
24+
// when we go to the required-course, and traverse it's neighbors, we want to make sure that those neighbor doesn't have others that nodes
25+
// that required those neighbor plus those neighbor's required-course
26+
// example [1,0], [0,2], [2,1]
27+
// 1 required 0, 0 required 2, and 2 required 1
28+
// it creates loop
29+
nodes[requiredCourse].neighbors.push(nodes[course]);
2230
}
2331
returnnodes;
2432
}
@@ -42,9 +50,13 @@ var canFinish = function(numCourses, prerequisites) {
4250
varnodes=constructGraph(numCourses,prerequisites);
4351

4452
for(vari=0;i<nodes.length;i++){
45-
console.log('nodes i',nodes[i])
46-
varhasCycle=dfs(nodes[i],[]);
53+
varparent=[];
54+
varhasCycle=dfs(nodes[i],parent);
55+
56+
console.log(hasCycle,i,nodes[i],parent)
4757
if(hasCycle)returnfalse;
4858
}
4959
returntrue;
50-
};
60+
};
61+
62+
canFinish(5,[[0,1],[1,2],[1,3],[1,4],[2,3]])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution(object):
2+
deffindKthLargest(self,nums,k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
9+
pivot=random.choice(nums);
10+
nums1,nums2= [], []
11+
fornuminnums:
12+
ifnum>pivot:
13+
nums1.append(num)
14+
elifnum<pivot:
15+
nums2.append(num)
16+
17+
ifk<=len(nums1):
18+
returnself.findKthLargest(nums1,k)
19+
ifk>len(nums)-len(nums2):# draw a graph to visualize it! It's not in the top k assortment, but in the small section
20+
returnself.findKthLargest(nums2,k- (len(nums)-len(nums2)))
21+
22+
returnpivot

‎289. Game of Life.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
*@param {number[][]} board
3+
*@return {void} Do not return anything, modify board in-place instead.
4+
*/
5+
vargameOfLife=function(board){
6+
// 3 over-population or under population
7+
// 2 means reproduction
8+
// 1 is alive
9+
// 0 is dead
10+
// if mod 2 === 1 means the current state is alive else dead
11+
12+
13+
varrows=board.length;
14+
varcols=board[0].length;
15+
16+
for(vari=0;i<rows;i++){
17+
for(varj=0;j<cols;j++){
18+
varcur=board[i][j];
19+
varcount=0;
20+
21+
if(i-1>=0&&j-1>=0&&(board[i-1][j-1])%2===1){
22+
count++;
23+
}
24+
if(i-1>=0&&(board[i-1][j])%2===1){
25+
count++;
26+
}
27+
if(i-1>=0&&j+1<cols&&(board[i-1][j+1])%2===1){
28+
count++;
29+
}
30+
if(j-1>=0&&(board[i][j-1])%2===1){
31+
count++;
32+
}
33+
if(j+1<cols&&(board[i][j+1])%2===1){
34+
count++;
35+
}
36+
if(i+1<rows&&j-1>=0&&(board[i+1][j-1])%2===1){
37+
count++;
38+
}
39+
if(i+1<rows&&(board[i+1][j])%2===1){
40+
count++;
41+
}
42+
if(i+1<rows&&j+1<cols&&(board[i+1][j+1])%2===1){
43+
count++;
44+
}
45+
46+
if(board[i][j]===1){
47+
if(count>3||count<2){
48+
board[i][j]=3;
49+
}
50+
}else{
51+
if(count===3){
52+
board[i][j]=2;
53+
}
54+
}
55+
}
56+
}
57+
58+
for(i=0;i<rows;i++){
59+
for(j=0;j<cols;j++){
60+
if(board[i][j]===3){
61+
board[i][j]=0;
62+
}elseif(board[i][j]===2){
63+
board[i][j]=1;
64+
}
65+
}
66+
}
67+
};

‎300 Longest Increasing Subsequence.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {number}
4+
*/
5+
varlengthOfLIS=function(nums){
6+
varsize=nums.length;
7+
8+
if(size===0){
9+
return0;
10+
}
11+
12+
vardp=Array.from({length:size},()=>1);
13+
14+
for(vari=0;i<size;i++){
15+
for(varj=0;j<i;j++){
16+
if(nums[i]>nums[j]){
17+
dp[i]=Math.max(dp[i],dp[j]+1);
18+
}
19+
}
20+
}
21+
22+
returnMath.max.apply(null,dp);
23+
};

‎335 Self Crossing.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*@param {number[]} x
3+
*@return {boolean}
4+
*/
5+
6+
// http://www.cnblogs.com/grandyang/p/5216856.html
7+
varisSelfCrossing=function(x){
8+
9+
for(vari=3;i<x.length;i++){
10+
if(x[i]>=x[i-2]&&x[i-3]>=x[i-1]){
11+
returntrue;
12+
}
13+
if(i>=4&&x[i-1]==x[i-3]&&x[i]>=(x[i-2]-x[i-4])){
14+
returntrue;
15+
}
16+
if(i>=5&&x[i-2]>=x[i-4]&&x[i-3]>=x[i-1]&&x[i-1]>=(x[i-3]-x[i-5])&&x[i]>=(x[i-2]-x[i-4])){
17+
returntrue;
18+
}
19+
}
20+
21+
returnfalse;
22+
};

‎37 Sudoku Solver.js

Whitespace-only changes.

‎4. Median of Two Sorted Arrays.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*@param {number[]} nums1
3+
*@param {number[]} nums2
4+
*@return {number}
5+
*/
6+
7+
// http://blog.csdn.net/yutianzuijin/article/details/11499917
8+
varfindMedianSortedArrays=function(nums1,nums2){
9+
varm=nums1.length;
10+
varn=nums2.length;
11+
vartotal=m+n;
12+
13+
if(total%2===1){
14+
returnfindKth(nums1,m,nums2,n,parseInt(total/2)+1);
15+
}else{
16+
return(findKth(nums1,m,nums2,n,parseInt(total/2))+findKth(nums1,m,nums2,n,parseInt(total/2)+1))/2;
17+
}
18+
};
19+
20+
21+
functionfindKth(a,m,b,n,k){
22+
// always assume that m is equal or smaller than n
23+
if(m>n){
24+
returnfindKth(b,n,a,m,k);
25+
}
26+
27+
if(m===0){
28+
returnb[k-1];
29+
}
30+
31+
if(k===1){
32+
returnMath.min(a[0],b[0]);
33+
}
34+
35+
// divide k into two parts
36+
varpa=Math.min(parseInt(k/2),m);
37+
varpb=k-pa;
38+
39+
if(a[pa-1]<b[pb-1]){
40+
returnfindKth(a.slice(pa),m-pa,b,n,k-pa);
41+
}elseif(a[pa-1]>b[pb-1]){
42+
returnfindKth(a,m,b.slice(pb),n-pb,k-pb);
43+
}else{
44+
returna[pa-1];
45+
}
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp