珠宝问题

嗯,就是大家看到的上面那个。已经完全通过了
你每次以为ok的时候,信心满满的submit的时候,worry!I’m sad.
先贴出来勉强可以解决(已经修改了,已经完全适应了。通过!):

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3.

Given “bvbf”, the answer is “vbf”, with the length of 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Created by YangSir on 2016/11/3.
*/

/**
* @param {string} s
* @return {number}
*/

var lengthOfLongestSubstring = function(s) {
var Arr = s.split("");
var Brr = [];
var Crr = [];
var Cto = 0;
var All = [Brr,Crr];
for(var i = 0, lenA = Arr.length; i<lenA; i++){
if(i===0){
All[Cto][0] = Arr[0];
continue;
}
for(var j = 0; j<All[Cto].length; j++){
if(All[Cto][j]==Arr[i]){
if(All[Cto].length>All[(Cto+1)%2].length){
// All[(Cto+1)%2] = All[Cto];
Cto = (Cto+1)%2;
All[Cto] = [];
for(var index = j+1,indexlen = All[(Cto+1)%2].length; index < indexlen; index++){
All[Cto].push(All[(Cto+1)%2][index]);
}
}else{
for(var kk = 0, index = j+1; kk < index; kk++){
All[Cto].shift();
}
}
All[Cto].push(Arr[i]);
j = j+All[Cto].length+1;
break;
}
}
if(j == All[Cto].length){
All[Cto].push(Arr[i]);
}
//console.log(All);
}
var result = All[0].length>All[1].length?All[0]:All[1];
//console.log(result);
var str = result.join("");
return str.length;
};
// lengthOfLongestSubstring("ggububgvfk");
// lengthOfLongestSubstring("jbpnbwwd");

改天再弄吧,好累。。。sad。。。(完成了,摩西摩西)