标签:class string temp nbsp 规律 return ret div str
1 //找规律题 等差数列 2m-2 2 class Solution 3 { 4 public: 5 string convert(string s, int m) 6 { 7 if(m == 1) return s; 8 int n = s.size(); 9 string res; 10 for(int i = 0;i < m;i ++) 11 { 12 if(i == 0 || i == m-1) 13 { 14 for(int k = 0;k <= n/m;k ++) 15 { 16 int temp = i + k*(2*m-2); 17 if(temp >= n) break; 18 res.push_back(s[temp]); 19 } 20 } 21 else 22 { 23 int a1 = i; 24 int b1 = 2*m-2-i; 25 for(int k = 0;k <= n/m;k ++) 26 { 27 int temp1 = a1 + k*(2*m-2); 28 if(temp1 >= n) break; 29 res.push_back(s[temp1]); 30 31 int temp2 = b1 + k*(2*m-2); 32 if(temp2 >= n) break; 33 res.push_back(s[temp2]); 34 } 35 } 36 } 37 return res; 38 } 39 };
标签:class string temp nbsp 规律 return ret div str
原文地址:https://www.cnblogs.com/yuhong1103/p/12499049.html