classSolution { public: string convert(string s, int numRows){ if (numRows <= 1) { return s; } int len_s = int(s.size()); int unit =(2*numRows-2); int n = len_s/unit; int remain = len_s%unit; string res(len_s, 0); for (int i = 0; i < len_s; i++) { int p = 0; if (i%unit == 0) { p = i/unit+1; } else { int r = i%unit + 1,c = i/unit+1; if (r > numRows) { r = unit-r+2; p = 1; } elseif (r == numRows) { p = 1-c; } p += n + (n*2)*(r-2) + 2*(c-1) + min(r-1, remain)+1; if (remain > numRows) { p += max(r-(unit-remain+2),0); } } res[p-1] = s[i]; } return res; } };
classSolution { public: intlengthOfLongestSubstring(string s){ if (s.length() == 1) return1; unordered_map<char, int> m; int len = s.length(); int count = 0; int max = 0; int i = 0, j = 0; for ( ; i < len; i++) { if (m[s[i]] < j + 1) { m[s[i]] = i + 1; } else { count = i - j; j = m[s[i]]; max = max > count ? max : count; m[s[i]] = i + 1; } } count = i - j; max = max > count ? max : count; return max; } };
classSolution { public: intfindPairs(vector<int>& nums, int k){ if (k < 0) return0; map<int, int> m; for (int x : nums) { m[x]++; } int ans = 0; auto ite = m.begin(); while (ite != m.end()) { if (k) { int sum = ite->first + k; if (m.count(sum)) ans++;//这里要用count函数查询是否存在元素,直接访问会超时 } else { if (ite->second > 1) ans++; } ite++; } return ans; } };