NC355. 划分字母区间
描述
示例1
输入:
"anfjaklii"
输出:
[5,1,1,2]
C++ 解法, 执行用时: 4ms, 内存消耗: 780KB, 提交时间: 2022-07-08
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: vector<int> splitString(string s) { int h[26]={0}; for(int i=0;i<s.size();i++) { h[s[i]-'a']=i; } vector<int>res; int l=0,r=0; for(int i=0;i<s.size();i++) { r=max(r,h[s[i]-'a']); if(i==r) { res.push_back(r-l+1); l=r+1; } } return res; } };
C++ 解法, 执行用时: 4ms, 内存消耗: 804KB, 提交时间: 2022-08-06
static const auto io_sync_off = [](){ std::ios::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return nullptr; }(); class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型vector */ vector<int> splitString(string s) { // write code here int h[26]={0}; for(int i=0;i<s.size();i++) { h[s[i]-'a']=i; } vector<int> res; int l=0,r=0; for(int i=0;i<s.size();i++) { r=max(r,h[s[i]-'a']); if(i==r) { res.push_back(r-l+1); l=r+1; } } return res; } };
Go 解法, 执行用时: 4ms, 内存消耗: 1164KB, 提交时间: 2022-05-29
package main // import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型一维数组 */ func splitString( s string ) []int { // write code here partition := make([]int, 0, 16) lastPos := [26]int{} for i, c := range s { lastPos[c-'a'] = i } start, end := 0, 0 for i, c := range s { if lastPos[c-'a'] > end { end = lastPos[c-'a'] } if i == end { partition = append(partition, end-start+1) start = end + 1 } } return partition } func partitionLabels(s string) (partition []int) { lastPos := [26]int{} for i, c := range s { lastPos[c-'a'] = i } start, end := 0, 0 for i, c := range s { if lastPos[c-'a'] > end { end = lastPos[c-'a'] } if i == end { partition = append(partition, end-start+1) start = end + 1 } } return }
C++ 解法, 执行用时: 5ms, 内存消耗: 772KB, 提交时间: 2022-07-24
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型vector */ vector<int> splitString(string s) { // write code here int n = s.size(); typedef pair<int,int> PII; vector<PII> mp(26); for (int i = 1; i <= n; i ++) { int idx = s[i - 1] - 'a'; if(mp[idx].first == 0) mp[idx].first = i; mp[idx].second = i; } sort(mp.begin(), mp.end()); //for(auto it : mp) cout << it.first << " " << it.second << endl; vector<int> res; vector<PII> merge; int start = 0, end = 0; for (int i = 0; i < 26; i ++) { auto x = mp[i].first, y = mp[i].second; if (x == 0) continue; if (start == 0) start = x, end = y; if(x <= end) end = max(end, y); else { merge.push_back({start, end}); start = x, end = y; } } merge.push_back({start, end}); cout << endl; //for(auto it : merge) cout << it.first << " " << it.second << endl; int m = merge.size(); for (int i = 0; i < m; i ++) { auto x = merge[i].first, y = merge[i].second; //cout << x << " xxxx " << y << endl; res.push_back((y - x + 1)); } return res; } };
C++ 解法, 执行用时: 5ms, 内存消耗: 776KB, 提交时间: 2022-07-03
class Solution { public: vector<int> splitString(string s) { vector<int> v(26); for (int i = 0; i < s.size(); ++i) { v[s[i] - 'a'] = i; } vector<int> res; int z = 0; int a = 0; for (int i = 0; i < s.size(); ++i) { z = max(z, v[s[i] - 'a']); if (i == z) { res.emplace_back(z - a + 1); a = i + 1; } } return res; } };