2017/11/5 Leetcode 日记

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Solutions:

class Solution {
public:
int findComplement(int num) {
unsigned mask = ~;
while(mask & num) mask <<=;
return ~mask & ~num;
}
};

c++

class Solution:
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
i =
while(i<=num):
i<<=
return (i-)^num

python3

557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Solutions:

class Solution {
public:
string reverseWords(string s) {
int index = , max;
for(int i = ; i < s.size(); i++){
if(s[i] == ' ' || i == s.size()-){
max = i-;
if (i == s.size()-) max = i;
for(int j = index; j < max; j++, max--){
char temp = s[j];
s[j] = s[max];
s[max] = temp;
}
index = i+;
}
}
return s;
}
};

c++

class Solution:
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
a = ' '
s = a.join(x[::-] for x in s.split())
return s

python3

500. Keyboard Row

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

(要你判断能用键盘一行打出来的单词,KeyboardMan必备技能啊)

Solutions:

class Solution {
public:
vector<string> findWords(vector<string>& words) {
unordered_set<char> set1 = {'q', 'w','e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
unordered_set<char> set2 = {'s', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'a'};
unordered_set<char> set3 = {'z', 'x', 'c', 'v', 'b', 'n', 'm'};
vector<unordered_set<char>> sets = {set1, set2, set3};
vector<string> nes;
for(string& word : words){
int raw = -;
for(int i = ; i<; i++){
if(sets[i].count(tolower(word[])) > )
raw = i;
}
if(raw == -) continue;//除0,1,2外的第四种可能
bool valid = true;
for(int j = , sz = word.size(); j < sz; j++){
if(sets[raw].count(tolower(word[j])) == )
valid = false;
}
if(valid)nes.push_back(word);
}
return nes;
}
};

c++

class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
return filter(re.compile('(?i)([qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*)$').match, words)

python2

2017/11/5 Leetcode 日记的更多相关文章

  1. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  2. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  3. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  4. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  5. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  6. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  7. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  8. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  9. jingchi.ai 2017.11.25-26 Onsite面试

    时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...

随机推荐

  1. MySQL性能优化之道

    1.in和not in子查询优化 not in 是不能命中索引的,所以以下子查询性能很低. 如果是确定且有限的集合时,可以使用.如 IN (0,1,2). 用 exists或 notexists代替 ...

  2. jquery 中$符号六大作用

    jquery 中$符号六大作用 2012-12-16 86市场网 javascript a.$用作选择器, var e = $("h1 a"); var f = $("t ...

  3. 网络抓包wireshark(转)

    下载地址:https://www.wireshark.org/download/win64/   抓包应该是每个技术人员掌握的基础知识,无论是技术支持运维人员或者是研发,多少都会遇到要抓包的情况,用过 ...

  4. 模型稳定度指标PSI与IV

    由于模型是以特定时期的样本所开发的,此模型是否适用于开发样本之外的族群,必须经过稳定性测试才能得知.稳定度指标(population stability index ,PSI)可衡量测试样本及模型开发 ...

  5. 利用Jsoup模拟跳过登录爬虫获取数据

    今天在学习爬虫的时候想着学习一下利用jsoup模拟登录.下面分为有验证码和无验证码的情况进行讨论. ---------------------------无验证码的情况---------------- ...

  6. Codeforces 870E Points, Lines and Ready-made Titles 计数

    题目链接 题意 给定二维坐标上的\(n\)个点,过每个点可以 画一条水平线 或 画一条竖直线 或 什么都不画,并且若干条重合的直线被看做同一条.问共可能得到多少幅不同的画面? 题解 官方题解 仆の瞎扯 ...

  7. Python 正则表达式、re模块

    一.正则表达式 对字符串的操作的需求几乎无处不在,比如网站注册时输入的手机号.邮箱判断是否合法.虽然可以使用python中的字符串内置函数,但是操作起来非常麻烦,代码冗余不利于重复使用. 正则表达式是 ...

  8. HBase原理解析(转)

    本文属于转载,原文链接:http://www.aboutyun.com/thread-7199-1-1.html   前提是大家至少了解HBase的基本需求和组件. 从大家最熟悉的客户端发起请求开始讲 ...

  9. bash: composer: command not found

    下载composer到本地:curl -sS https://getcomposer.org/installer | php 移动至系统服务:sudo mv composer.phar /usr/bi ...

  10. unittest单元测试

    unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...