public class Solution
{
public IList<int> PartitionLabels(string S)
{
var dic = new Dictionary<char, int[]>();
//记录每一个字符的第一次出现位置,和最后一次出现位置
for (int i = ; i < S.Length; i++)
{
if (!dic.ContainsKey(S[i]))
{
dic.Add(S[i], new int[] { i, i });
}
else
{
dic[S[i]][] = i;
}
}
var list = new List<int>();
int low = ;
int high = S.Length - ;
while (low <= high)
{
var c = S[low];
var i = dic[c][];//当前字符最小索引
var j = dic[c][];//当前字符最大索引
if (j == high)
{
list.Add(high - low + );
return list;
}
for (; i <= j; i++)
{
var cc = S[i];
var ii = dic[cc][];
var jj = dic[cc][];
if (jj == high)
{
list.Add(high - low + );
return list;
}
if (jj > j)
{
j = jj;
}
}
list.Add(i - low);
low = i;
} return list;
}
}

本题使用贪心算法思想,这里给出的代码是比较高效的一种解法。

leetcode763的更多相关文章

  1. [Swift]LeetCode763. 划分字母区间 | Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  2. 贪心算法leetcode-763

    int[] lastShow = new int[26]; var list = new LinkedList<Integer>(); for (int i = 0; i < s.l ...

随机推荐

  1. 彻底明白IP地址——计算相关地址

    知道ip地址和子网掩码后可以算出: 1. 网络地址 2. 广播地址 3. 地址范围 4. 本网有几台主机 例1:下面例子IP地址为192·168·100·5 子网掩码是255·255·255·0.算出 ...

  2. [转载]Java抽象类和接口的学习

    http://android.blog.51cto.com/268543/385282/ 抽象类 abstract class     包含抽象方法的类,叫抽象类.而抽象的概念就是抽象出共同属性:成员 ...

  3. 【转】HTTP协议—— 简单认识TCP/IP协议

    转自:www.cnblogs.com/roverliang/p/5176456.html 大学没读计算机专业,所以很多的专业知识都不知道.既然已经从事了IT这个行业,就势必要去了解下网络底层,虽然实际 ...

  4. java- Collection Set集合

    首先HashSet类创建集合对象和遍历对象 package set; import java.util.HashSet; import java.util.Iterator; //hashset储存字 ...

  5. 转:Android-apt

    转自http://blog.csdn.net/zjbpku/article/details/22976291 What is this? The Android-apt plugin assists ...

  6. 让黑白的SecureCRT彩色起来

    让黑白的SecureCRT彩色起来,如图仿真设置如下:  

  7. iis6 , URL重写HTM文件名后,出现真实的HTM文件不能访问的解决

    服务器环境是windows 2003 IIS6 在web.config文件中加入 1.在<compilation debug="true"> 节点加入 <buil ...

  8. 一个Android开发妹子的找工作心酸史

    阿里:实习二面被KO 1,人生接到的第一个电话面试来自大家都说好的阿里,心情激动,说话颤抖,本以为没有戏,然而第二天接到了二面的电话有点小激动.然后就是被ko了,死的原因很简单,那时候单纯的自己什么都 ...

  9. test20181024 hao

    题意 分析 考场10分 直接\(O(nm)\)模拟即可. #include<cstdlib> #include<cstdio> #include<cmath> #i ...

  10. conan c&&c++ 包管理工具使用

    测试使用的是JFrog Artifactory CE 进行的私有包管理,具体的安装可以参考相关文档 启动JFrog Artifactory CE 使用docker docker run -d -p 8 ...