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. iOS开发系列-线程状态

    概述 线程从创建到销毁中间存在很多种状态. 线程的状态 通过NSThread创建一条线程,开发者需要负责线程的创建和执行,线程的销毁由系统决定.创建一个继承NSThread的FMThread类,重写d ...

  2. 【案例】DIV随鼠标移动而移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 判断JS对象是否为空的几种方式

    .将json对象转化为json字符串,再判断该字符串是否为"{}" var data = {}; var b = (JSON.stringify(data) == "{} ...

  4. MySQL数据库CRUD命令用法

    数据库CRUD操作即添加(Create).读取(Read).更新(Update)和删除(Delete). 1. 添加操作也称插入操作,使用Insert语句,Insert语句可以用于几种情况: 插入完整 ...

  5. [JZOJ5232] 【NOIP2017模拟A组模拟8.5】带权排序

    题目 题目大意 有一个数列AAA,数列上的每个数都是在[li,ri][l_i,r_i][li​,ri​]范围内随机的数. 将这个数列进行稳定排序,得到每个位置在排序后的排名pip_ipi​. f(A) ...

  6. python事件调度库sched

    事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...

  7. Java-Druid:目录

    ylbtech-Java-Druid:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://yl ...

  8. Django logging 的配置方法

    做开发离不开日志,以下是我在工作中写Django项目常用的logging配置. BASE_LOG_DIR = os.path.join(BASE_DIR, "log") LOGGI ...

  9. Axure教程:如何使用动态面板?动态面板功能详解

    写了几个Axure教程之后发现,可能教程的起点有些高了,过分的去讲效果的实现,而忽略了axure功能以及基础元件的使用,那么从这个教程开始,把这些逐渐的展开讲解. 关于Axure动态面板 动态面板是a ...

  10. RQNOJ--2 开心的金明(01背包)

    题目:http://www.rqnoj.cn/problem/2 分析:这个题目每一种物品都是有"选"或"不选"两种情况. 属于01背包问题.物品的价格相当于背 ...