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. CF821 A. Okabe and Future Gadget Laboratory 水

    Link 题意:询问n X n中非1数是否能够由同行同列中分别取两个数做和得到. 思路:水题. /** @Date : 2017-07-03 16:23:18 * @FileName: A.cpp * ...

  2. PHP扩展--taint检测隐藏漏洞

    简介 Taint 可以用来检测隐藏的XSS code, SQL注入, Shell注入等漏洞, 并且这些漏洞如果要用静态分析工具去排查, 将会非常困难, 比如对于如下的例子: <?php echo ...

  3. JAVA中反射机制六(java.lang.reflect包)

    一.简介 java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口.反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问.它允许在安全限制内使用反射的字段, ...

  4. 数据库 插入时 碰到NULL报错判断的一种方法(技巧)

    //public static object ToDBNull(object value) 判断插入数据的时候个别参数不能为空的时候做的判断方法 //{ // if (value == null) / ...

  5. jQuery代码优化:基本事件

    jQuery对事件系统的抽象与优化也是它的一大特色.本文仅从事件系统入手,简要分析一下jQuery为什么提供mouseenter和mouseleave事件,它们与标准的mouseover.mouseo ...

  6. 【CodeForces】576 B. Invariance of Tree

    [题目]B. Invariance of Tree [题意]给定n个数的置换,要求使n个点连成1棵树,满足u,v有边当且仅当a[u],a[v]有边,求一种方案或无解.n<=10^5. [算法]数 ...

  7. 【leetcode 简单】第十七题 x 的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  8. HDU 2191 珍惜现在,感恩生活 (dp)

    题目链接 Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都 ...

  9. SQLite3使用详解

    sqlite常量的定义(SQLite3返回值的意思): SQLITE_OK           = 0;  返回成功 SQLITE_ERROR        = 1;  SQL错误或错误的数据库 SQ ...

  10. 读书笔记 effective c++ Item 3 在任何可能的时候使用 const

    Const可以修饰什么?   Const 关键字是万能的,在类外部,你可以用它修饰全局的或者命名空间范围内的常量,也可以用它来修饰文件,函数和块作用域的静态常量.在类内部,你可以使用它来声明静态或者非 ...