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

Commitdbcd817

Browse files
author
Chihung Yu
committed
p1
1 parent40e026a commitdbcd817

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

‎159 Longest Substring with At Most Two Disctinct Characters.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*@param {string} s
55
*@return {number}
66
*/
7+
8+
// Time O(N)
9+
// Space O(1)
710
varlengthOfLongestSubstringTwoDistinct=function(s){
811
varlongestSubstr="";
912
varmaxLength=0;
@@ -15,15 +18,14 @@ var lengthOfLongestSubstringTwoDistinct = function(s) {
1518

1619
// if map already contains two distrinct chars and the char is new to the map
1720
if(map.size>1&&map.get(c)===undefined){
18-
varleftMost=s.length;
19-
2021
// Calc substring len before the new char
2122
if(i-start>maxLength){
2223
// Should not include i, since i is the new distinct char's index
2324
longestSubstr=s.substring(start,i);
2425
maxLength=longestSubstr.length;
2526
}
26-
27+
// Find the left most char
28+
varleftMost=s.length;
2729
map.forEach((charIdx,key)=>{
2830
if(charIdx<leftMost){
2931
leftMost=charIdx;
@@ -44,3 +46,43 @@ var lengthOfLongestSubstringTwoDistinct = function(s) {
4446

4547
returnmaxLength;
4648
};
49+
50+
51+
52+
53+
/**
54+
*@param {string} s
55+
*@return {number}
56+
*/
57+
varlengthOfLongestSubstringTwoDistinct=function(s){
58+
varmap=newMap();
59+
varstart=0;
60+
varmaxLen=0;
61+
62+
for(vari=0;i<s.length;i++){
63+
varc=s[i];
64+
if(map.size===2&&map.get(c)===undefined){
65+
varcurStr;
66+
if(i-start>maxLen){
67+
curStr=s.substring(start,i);
68+
maxLen=i-start;
69+
}
70+
varleftMost=s.length;
71+
map.forEach((charIdx,key)=>{
72+
if(charIdx<leftMost){
73+
leftMost=charIdx;
74+
}
75+
});
76+
start=leftMost+1;
77+
map.delete(s[leftMost]);
78+
}
79+
80+
map.set(c,i);
81+
}
82+
83+
if(s.length-start>maxLen){
84+
maxLen=s.length-start;
85+
}
86+
87+
returnmaxLen;
88+
};

‎48 Rotate Image.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,42 @@ var swap = function(matrix, i1, j1, i2, j2) {
7171
matrix[i2][j2]=aux;
7272
};
7373

74+
75+
76+
77+
/**
78+
*@param {number[][]} matrix
79+
*@return {void} Do not return anything, modify matrix in-place instead.
80+
*/
81+
varrotate=function(matrix){
82+
rotateDiagonal(matrix);
83+
rotateRow(matrix);
84+
};
85+
86+
varswap=function(matrix,x1,y1,x2,y2){
87+
vartemp=matrix[x1][y1];
88+
matrix[x1][y1]=matrix[x2][y2];
89+
matrix[x2][y2]=temp;
90+
}
91+
92+
varrotateRow=function(matrix){
93+
for(vari=0;i<matrix.length;i++){
94+
varrow=matrix[i];
95+
varstart=0;
96+
varend=matrix[i].length-1;
97+
98+
while(start<end){
99+
swap(matrix,i,start,i,end);
100+
start++;
101+
end--;
102+
}
103+
}
104+
}
105+
106+
varrotateDiagonal=function(matrix){
107+
for(vari=0;i<matrix.length;i++){
108+
for(varj=i;j<matrix.length;j++){
109+
swap(matrix,i,j,j,i);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp