leetcode-15-basic-string
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的更多相关文章
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 黑马程序猿——15,String,StringBuffer,基本数据类型包装对象
------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LINQ to SQL语句(15)之String
LINQ to SQL支持以下String方法.但是不同的是默认情况下System.String方法区分大小写.而SQL则不区分大小写. 1.字符串串联(String Concatenation) v ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- 1137 - Sin your life sin公式 + 枚举
http://www.ifrog.cc/acm/problem/1137 和差化积公式, 变成2 * sin((x + y) / 2) * cos((x - y) / 2) + sin(n - (x ...
- 洛谷P1965 转圈游戏
https://www.luogu.org/problem/show?pid=1965 快速幂 #include<iostream> #include<cstdio> #inc ...
- 编译安装php容易出现的问题以及解决办法
http://crybit.com/20-common-php-compilation-errors-and-fix-unix/
- 轻量级的绘制图表js库--Morris.js
Morris.js 是一个轻量级的 JS 库,使用 jQuery 和 Raphaël 来生成各种时序图. 虽说现在移动手机网络已经到了4G,但是在移动web客户端开发过中,为了达到良好的体验效果,需要 ...
- Kendo UI Validator 概述
Kendo UI Validator 概述 Kendo UI Validator 支持了客戶端校驗的便捷方法,它基於 HTML 5 的表單校驗功能,支持很多內置的校驗規則,同時也提供了自定義規則的便捷 ...
- UI事件与内容,舞台与演员
UI事件:创建/清除/显示/隐藏/填充内容/位置变化/形态变化/尺寸变化/颜色变化/ 非UI事件:点击/输入/拖动/
- 字符串(String)杂谈
作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com >>>转载请注明出处!<<< 上一次我们已经一起回顾了面试题 ...
- windows网络和共享中心“查看基本网络信息并设置连接”为“未知”的解决方案
存在问题“查看基本网络信息并设置连接”为“未知”.如图所示: 解决步骤 运行services.msc 启动Network List Service 若无法启动,打开其属性,选择“登录”选项卡,将启动类 ...
- 跑yscacaca/HHAR-Data-Process出现的问题
直接按照说明跑: python dataAli-sameUserDevice.py python pairDataFile.py python sep_HHAR_data.py 但在sep_HHAR_ ...
- python 遍历list
#!/usr/bin/env python# -*- coding: utf-8 -*-if __name__ == '__main__': list = ['html', 'js', 'css ...