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. socket.io入门示例参考

    参考示例地址:http://www.linchaoqun.com/html/cms/content.jsp?menu=nodejs&id=1480081169735

  2. Ubuntu下安装Yarm-PM2

    首先打开yarm的官网.https://www.yarnpkg.com/zh-Hant/ (一)yarn的官方安装方法: 1.上通过 Debian 套件安裝 Yarn,粘贴以下命令 curl -sS ...

  3. 如何在windows下是用mysqldumpslow命令

    1. 再一次点击mysql安装文件(默认是没安装mysqldumpslow这些脚本的),如图: 点击next如下图 点击Developer Components 旁边的选择this feature , ...

  4. 洛谷 P1137 旅行计划

    旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  5. ArcGIS for Server内置JS Viewer的离线部署和配置

    很多情况下,在地图服务发布完毕后,我们往往利用 ArcGIS for Server内置的 JS Viewer来查看和检测所发布的地图服务是否满足我们的要求.具体操作如下: 点击开始 -> 所有程 ...

  6. Python3+Selenium3+webdriver学习笔记6(多窗口切换处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriverfrom selenium.webdriver.co ...

  7. Python+selenium整合自动发邮件功能

    主要实现的目的是:自动将测试报告以邮件的形式通知相关人员 from HTMLTestRunner import HTMLTestRunner import HTMLTestReport from em ...

  8. github的pull Request使用

    场景: teamA要一起做一个项目,选择用github管理自己的代码仓库,这时userA在github上新建了一个远程仓库,其他人需要通过pull request来实现提交.那么,问题来了,pull ...

  9. mysql数据库备份/恢复

    备份数据库(进入Mysql bin目录下/C:\Program Files\MySQL\MySQL Server 5.6\bin)本地安装mysql数据库 备份表结构及数据 mysqldump -hl ...

  10. 使用代码获得Netweaver里某个software component和C4C的版本

    有同事问如何通过代码的方式获得Netweaver里某个Software component的版本信息,以及Cloud for Customer(C4C)的版本信息. Netweaver 点了Detai ...