记录一次数组越界的bug


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;

class Solution {
public:
int n;
string ss;
void fn(vector<int>& v,int i){
if(i<0||i>=n)return ;
int index=i;
i--;
while(i>=0&&i<n){
//cout<<i<<endl;
if(v[i]!=0){
//cout<<index<<endl;
if(i>0&&i<n-1){
v[i]=(v[i+1]<v[i-1]?v[i+1]:v[i-1])+1;
}else if(i==0){
v[i]=v[i+1]+1;
}else if(i==n-1){
v[i]=v[i-1]+1;
}else{
break;
}
i=i--;
if(i==-1)return ;
cout<<i<<endl;
}else break;
}
index++;
while(index>=0&&index<n){
//cout<<index<<endl;
if(v[index]!=0){
//cout<<index<<endl;
if(index>0&&index<n-1){
v[i]=(v[index+1]<v[index-1]?v[index+1]:v[index-1])+1;
}else if(index==0){
v[index]=v[index+1]+1;
}else if(index==n-1){
v[index]=v[index-1]+1;
}else{
break;
}
index++;
}else break;
}

}
vector<int> shortestToChar(string s, char c) {
ss=s;
queue<int> que;
n=s.length();
cout<<n<<endl;
vector<int> v(20,20);
// for(int i=0;i<v.size();i++){
// cout<<v[i]<<" ";
// }
for(int i=0;i<n;i++){
if(s[i]==c){
v[i]=0;
que.push(i);
}
}
while(!que.empty()){
fn(v,que.front());
que.pop();
}
return v;
}
};
int main(){
Solution *s = new Solution();
vector<int> v = s->shortestToChar("loveleetcode",'e');
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
return 0;
}

Author: pkq
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source pkq !
  TOC