58. Length of Last Word

解题思路:

从结尾向前搜索,空格之前的就是最后一个词了。写的时候我考虑了尾部有空格的情况。需要注意的是,测试用例中有" "的情况,此时应返回0。

int lengthOfLastWord(string s) {
if (s.length() == 0 || s.length() == 1 && s[0] == ' ')
return 0;
if (s.length() == 1 && s[0] != ' ')
return 1;
int i;
int count = 0;
bool flag = false;
for (i = s.length() - 1; i >= 0; i--) {
if (s[i] == ' ') {
if (flag == false)
continue;
else
break;
}
if (s[i] != ' ') {
if (flag == false)
flag = true;
count++;
}
}
return count;
}  

338. Counting Bits

解题思路:

这道题想了好久。。思路是当前数i与i-1按位做与操作,这样可以找到最长相同前缀的地方,从那个位置之后,i-1的为0,i的为1且后面都是0,所以只要

取这个数的1的个数再加1就可以了。例如:

1011 0011 1

1011 0100 0

所以只要知道1011 0000 0处1的个数,再加1即可。用vector存好即可。

vector<int> countBits(int num) {
vector<int> result(num+1, 0);
if (num == 0)
return result;
for (int i = 1; i < num + 1; i++) {
result[i] = result[i & (i - 1)] + 1;
}
return result;
}

leetcode-15-basic-string的更多相关文章

  1. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  2. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  3. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  4. 黑马程序猿——15,String,StringBuffer,基本数据类型包装对象

    ------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...

  5. [LeetCode] 772. Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. [LeetCode] 227. Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  7. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  8. LINQ to SQL语句(15)之String

    LINQ to SQL支持以下String方法.但是不同的是默认情况下System.String方法区分大小写.而SQL则不区分大小写. 1.字符串串联(String Concatenation) v ...

  9. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  10. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

随机推荐

  1. idea svn操作

    https://blog.csdn.net/bug_love/article/details/72875511

  2. 050 Pow(x, n)

    实现 pow(x, n).示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100详见:https://leetcode.com ...

  3. C#关键字:yield

    yield是C#为了简化遍历操作实现的语法糖.在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.有两种形式: yield return <expres ...

  4. hashlib(加盐)回炉练习

    简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已经废弃了md5和sha模块,简单说明 ...

  5. java核心技术 - 17个重要的知识点

    1.Java中没有多继承,而是用接口来代替多继承 2.运行一个已经编译的程序时,Java解释器总是从指定类的main方法中的代码开始执行,因此,执行代码中必须有一个main函数. 3.Java是典型的 ...

  6. 数据库迁移后报错提示MySQL Error:Can''t find file errno: 13 - Permission denied的解决方法

    用户MYSQL数据库迁移后,遇到报错MySQL Error:Can't find file (errno: 13 - Permission denied)使用以下指令重新设置所有者和权限,依然不能解决 ...

  7. HDU 3681 Prison Break 越狱(状压DP,变形)

    题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...

  8. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  9. 融云红包全新升级,让App用户更便捷地用“钱”交流感情!

    随着移动互联网的飞速发展,如何增强社交关系.留住用户的心已成为移动社交化时代各类App持续探索的问题,除了接入即时通讯的能力,众多社交平台开始通过趣味性十足的红包功能为App中的社交场景赋能.当即时通 ...

  10. 如何使用Python生成200个优惠券(激活码)

    解析: 常见的优惠券(激活码)是由数字.字母(大小写)组成: string.ascii_letters   26个大小写字母: string.digits 0-9数字: 随机组合 使用random.s ...