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. 重构改善既有代码设计--重构手法13:Inline Class (将类内联化)

    某个类没有做太多事情.将这个类的所有特性搬移到另一个类中,然后移除原类. 动机:Inline Class (将类内联化)正好于Extract Class (提炼类)相反.如果一个类不再承担足够责任.不 ...

  2. 20155117王震宇 2006-2007-2 《Java程序设计》第二周学习总结

    学号 2006-2007-2 <Java程序设计>第X周学习总结 教材学习内容总结 学习一门语言首先要熟悉基础的语法,注意不要和之前学过的语言知识混淆. java严格区分大小写. 教材学习 ...

  3. MongoDB中的基础概念:Databases、Collections、Documents

    MongoDB以BSON格式的文档(Documents)形式存储.Databases中包含集合(Collections),集合(Collections)中存储文档(Documents). BSON是一 ...

  4. 【洛谷 P3299】 [SDOI2013]保护出题人 (凸包,三分,斜率优化)

    题目链接 易得第\(i\)关的最小攻击力为\(\max_{j=1}^i\frac{sum[i]-sum[j-1]}{x+d*(i-j)}\) 十分像一个斜率式,于是看作一个点\(P(x+d*i,sum ...

  5. 41、用Python实现一个二分查找的函数

    data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def binary_search(dataset ...

  6. Java后台开发面试题总结

    1>如何定位线上服务OOM问题 2>JVM的GC ROOTS存在于那些地方 3>mysql innodb怎样做查询优化 4>java cas的概念 Java服务OOM,比较常见 ...

  7. Jsoup爬取带登录验证码的网站

    今天学完爬虫之后想的爬一下我们学校的教务系统,可是发现登录的时候有验证码.因此研究了Jsoup爬取带验证码的网站: 大体的思路是:(需要注意的是__VIEWSTATE一直变化,所以我们每个页面都需要重 ...

  8. ogg使用语句

    create tablespace ogg datafile '/oracle/oradata/DRMT/ogg01.dbf' size 50M autoextend on; edit params ...

  9. 《深入理解Java虚拟机》笔记--第十二章、Java内存模型与线程

    主要内容:虚拟机如何实现多线程.多线程之间由于共享和竞争数据而导致的一系列问题及解决方案. Java内存模型:     Java内存模型的主要目标是定义程序中各个变量的访问规则,即在虚拟机中将变量存储 ...

  10. fullpage.js 具体使用方法

    1.fullpage.js  下载地址 https://github.com/alvarotrigo/fullPage.js 2.fullPage.js 是一个基于 jQuery 的插件,它能够很方便 ...