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;
}
};
 461. Hamming Distance
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的更多相关文章

  1. 【LeetCode】476. 数字的补数 Number Complement

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...

  2. 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 ...

  3. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  4. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  5. 【leetcode】476. Number Complement

    problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...

  6. Java实现 LeetCode 448 找到所有数组中消失的数字

    448. 找到所有数组中消失的数字 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间 ...

  7. [LeetCode] 448. 找到所有数组中消失的数字(思维)

    题目 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. LeetCode 19.删除链表的倒数第N个节点(Python)

    题目:    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点 ...

  2. 2019-11-9-win10-支持默认把触摸提升-Pointer-消息

    title author date CreateTime categories win10 支持默认把触摸提升 Pointer 消息 lindexi 2019-11-09 15:32:31 +0800 ...

  3. Leetcode166. Fraction to Recurring Decimal分数到小数

    给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...

  4. Bootstrap Paginator分页插件(mark)

    Bootstrap Paginator分页插件

  5. 使用fileupload实现文件上传

    一. fileupload组件工作原理 先来张图片, 帮助大家理解 fileupload核心API 1. DiskFileItemFactory构造器1) DiskFileItemFactory() ...

  6. springboot与任务(定时任务)

    描述: 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler ...

  7. CentOS7使用firewalld打开关闭防火墙与端口(转)

    CentOS7使用firewalld打开关闭防火墙与端口       1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop ...

  8. Mybatis-configuration 配置-settings配置

    设置参数 描述 有效值 默认值 cacheEnabled 这个配置使全局的映射器启用或禁用 缓存. true | false true lazyLoadingEnabled 全局启用或禁用延迟加载.当 ...

  9. Spring boot获取yml字段内容为null的各种情况

    首先,在resource目录下配置test.yml文件 A: B: http://123.com? C: username="lili"&password="12 ...

  10. myeclipse中tomcat内存大小的设置

    刚刚安装了myeclipse9.0,又配置了tomcat7.0,想用ssh框架搭个项目试试tomcat7.0,没想到刚启动项目就会报错,在tomcat6.0中就不会有问题,上网查了那些都不起作用,后来 ...