leetcode 448 - 476
448. Find All Numbers Disappeared in an Array
Input:
[4,3,2,7,8,2,3,1] Output:
[5,6]
思路:把数组的内容和index进行一一对应映射,映射规则是取反,由此可知,重复出现两次的数字会变为正,出现一次的为负。
需要注意的是,如果不能增加额外空间的话,要在本数组上面进行映射,这时候就需要内容-1=index,因为index是从0开始,内容从1开始
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int len = nums.size();
for(int i=; i<len; i++) {
int m = abs(nums[i])-; // index start from 0
nums[m] = nums[m]> ? -nums[m] : nums[m];
}
vector<int> res;
for(int i = ; i<len; i++) {
if(nums[i] > ) res.push_back(i+);
}
return res;
}
};
455. Assign Cookies
Input: [1,2], [1,2,3] Output: 2 Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
思路:先对小孩和饼干进行排序,逐个对应匹配。若当前小孩符合,则下一个小孩和下一个饼干;否则下一个饼干匹配当前小孩。
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int i = , j=;
while(i<g.size() && j<s.size()){
if(s[j]>=g[i])
i++; // when the child get the cookie, foward child by 1
j++;
}
return i;
}
};
Input: x = 1, y = 4 Output: 2 Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ The above arrows point to positions where the corresponding bits are different.
class Solution {
public:
int hammingDistance(int x, int y) {
int dist = , n = x ^ y; //按位异或
while (n) {
++dist;
n &= n - ; //效果循环右移,左补零
}
return dist;
}
};
459. Repeated Substring Pattern
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
思路:
法一:从str的一半开始,用
class Solution {
public:
bool repeatedSubstringPattern(string str) {
string nextStr = str;
int len = str.length();
if(len < ) return false;
for(int i = ; i <= len / ; i++){
if(len % i == ){ //若能整除,表示源字符串可以划分成正数个子字符串
nextStr = leftShift(str, i); //将源字符串左移一个子字符串,若左移后和源字符串相等,则判断为符合;这里也可以使用一个子字符串乘以个数得到的串和源字串比较
if(nextStr == str) return true;
}
}
return false;
} string leftShift(string &str, int l){
string ret = str.substr(l);
ret += str.substr(, l);
return ret;
}
};
法二:
将源字符串变成两倍,然后去掉首尾字符,剩下的串里面寻找源字串,若存在则判符合。
内部思想,如果源字符串不存在重复子字符串,则两倍后的字符串去掉前后字母后,里面必定不存在源字符串;否则必定存在源字符串。
bool repeatedSubstringPattern(string str)
{
return (str + str).substr(, str.size() * - ).find(str)!=-;
}
476. Number Complement
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
//使用mask来统计num二进制的位数
class Solution {
public:
int findComplement(int num) {
unsigned mask = ~;
while (num & mask) mask <<= ;
return ~mask & ~num;
}
};
For example, num =
mask =
~mask & ~num =
//使用i来统计num二进制的位数
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num
leetcode 448 - 476的更多相关文章
- 【LeetCode】476. 数字的补数 Number Complement
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...
- LeetCode 448. Find All Numbers Disappeared in an Array (在数组中找到没有出现的数字)
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- Java实现 LeetCode 448 找到所有数组中消失的数字
448. 找到所有数组中消失的数字 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间 ...
- [LeetCode] 448. 找到所有数组中消失的数字(思维)
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...
- LeetCode "448. Find All Numbers Disappeared in an Array"
My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...
- 5. Leetcode 448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
随机推荐
- asp.net去除HTML标签
public string NoHTML(string Htmlstring) //替换HTML标记 { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @ ...
- sqllocaldb
创建实例 sqllocaldb create v12.0 启动实例 sqllocaldb start v12.0
- android 遍历控件
做个笔记 androuid 遍历一个 view 下面的子view // 保存 btnSaveRout.setOnClickListener(new OnClickListener() { @Overr ...
- CI框架 session 不能读取的问题,PHP7环境
根本原因在这,libraries/Session/Session.php 中 128行: 如果sessionid的长度不是40的话,每次执行都会 unset($_COOKIE[ci_session]) ...
- GCC 参数详解
转载出处:http://blog.csdn.net/yff1030/article/details/8592077 原文:http://www.cppblog.com/SEMAN/archive/20 ...
- Linux查看温度
step 1: centos $ sudo yum install lm_sensors ubuntu $ sudo apt-get install lm_sensors step2$ sudo se ...
- js如何往数组Array中添加元素 (2013-09-04 10
unshift:将参数添加到原数组开头,并返回数组的长度 pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined push:将参数添加到原数组末尾,并返回数组的长度 co ...
- 编程之法:面试和算法心得(字符串包含java实现)
内容全部来自编程之法:面试和算法心得一书,实现是自己写的使用的是java 题目描述 给定两个分别由字母组成的字符串A和字符串B,字符串B的长度比字符串A短.请问,如何最快地判断字符串B中所有字母是否都 ...
- vue-admin-template模板添加screenfull全屏插件
先安装screenfull:npm install screenfull@4.2.0 --save (注意版本号) 一.在\src\components目录下创建Screenfull文件夹,创建 ...
- shell下时间日期的加减乘除运算
首先我们先来说说什么是shell下的时间戳: 自1970年1月1日(00:00:00 UTC/GMT)以来的秒数.它也被称为Unix时间戳(Unix Timestam.Unix epoch.POSIX ...