2017/11/5 Leetcode 日记
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:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- 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 日记的更多相关文章
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
- jingchi.ai 2017.11.25-26 Onsite面试
时间:2017.11.25 - 11.26 地点:安徽安庆 来回路费报销,住宿报销. day1: 大哥哥问了我一个实际中他们遇到的问题.有n个点,将点进行分块输出,输出各个块的均值点.具体就是100* ...
随机推荐
- 816E. Karen and Supermarket 树形DP
LINK 题意:给出n个商品,除第一个商品外,所有商品可以选择使用优惠券,但要求其前驱商品已被购买,问消费k以下能买几个不同的商品 思路:题意很明显就是树形DP.对于一个商品有三种选择,买且使用优惠券 ...
- JAVA中反射机制四
声明:如需转载请说明地址来源:http://www.cnblogs.com/pony1223 反射四 利用反射获取类的属性 1.通过反射也可以获取到类中的属性,假设我们继续使用Person这个类,然后 ...
- ⑥ 设计模式的艺术-06.建造者(Builder)模式
场景 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样一个问题需要处理: 装配这些子组件是不是有个步骤问题? 实际开发中,我们所需要的对象构建时,也非常复杂,有很多 ...
- C# 遍历枚举
C#中,如何获取(遍历)枚举中所有的值: public enum Suits { Spades, Hearts, Clubs, Diamonds, NumSuits } private static ...
- Js冒泡事件详解及阻止
Js冒泡机制是指如果某元素定义了事件A,如click事件,如果触发了事件之后,没有阻止冒泡事件,那么事件将向父级元素传播,触发父类的click函数. 如下例所示: <html> & ...
- python碎片记录(三)
1.不换行输出 for i in range(5): print(i,end=' ')不换行打印,end表示每打印一个后面跟的字符 2.利用枚举方式打印输出索引与数值 a=[7,8,9]for ...
- 老版本ubuntu更新源地址以及sources.list的配置方法 转
转自(http://blog.csdn.net/snaking616/article/details/52966634) 1.国内可用的更新源地址: (1)中科大地址 http://mirrors.u ...
- slave->pxc后GTID不一致
以下两个参数在两个节点是对得上的. | wsrep_last_applied | 3363764 | | wsrep_last_committed | 3363764 但show master sta ...
- logging模块配置笔记
logging模块配置笔记 log文件的路径 #判断在当前的目录下是否有一个logs文件夹.没有则创建 log_dir = os.path.dirname(os.path.dirname(__file ...
- openjudge-NOI 2.6-1768 最大子矩阵
题目链接:http://noi.openjudge.cn/ch0206/1768/ 题解: 如果用O(n4)的算法肯定会炸,需要压缩掉一维的空间,只需要简单加和就好啦 例如,我们要对样例中第2-4行D ...