杨帆博客

你想开发游戏还是改变世界?


  • 首页

  • 标签

  • 归档

  • 搜索

Intersection-of-Two-Arrays

发表于 2017-12-15 | 更新于: 2018-08-22

LeetCode#349 Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

解题思路

  没什么说的,直接遍历查找,找到就存值,直接看代码。

解题代码【.CPP】

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
std::set<int> rset;
for (auto it = nums1.begin() ; it != nums1.end() ; ++it){
if(std::find(nums2.begin() , nums2.end() , *it) != std::end(nums2)){
rset.insert(*it);
}
}
return std::vector<int>(rset.begin(),rset.end());
}
};

Intersection-of-Two-Arrays-II

发表于 2017-12-15 | 更新于: 2018-08-22

LeetCode#350 Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1‘s size is small compared to nums2‘s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

解题思路

  之前有一道题也是求两个数组的交点Intersection of Two Arrays,不过那个题比较简单,只需要循环然后使用std::find查找就可以了。这个题需要有重复的,我们可以将第一个数组的值和出现的次数存到map里,然后遍历第二个数组,如果在map里出现过就存入结果集里,并且将出现次数减一。

解题代码【.CPP】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int , int> um;
for (auto num : nums1){
if(um.find(num) != um.end()) ++um[num];
else um[num] = 1;
}
vector<int> ret(0);
for (auto num: nums2) {
if(um[num] > 0){
ret.push_back(num);
--um[num];
}
}
return ret;
}
};

【OpenGL】4-VAO与VBO

发表于 2017-12-14 | 更新于: 2018-08-22

VAO与VBO来历

  首先给出参考资料:VAO与VBO的前世今生

  VAO(Vertex Arrays Object):顶点数组对象

  VBO(Vertex Buffer Object):顶点缓冲对象

  首先由其名称就可以知道,这两个是用来操作顶点的,我们的显示器就是一个大的网格,而我们所看到的界面便是顶点和顶点周围所对应的颜色组成的,在上篇文章中我们使用了两个着色器,顶点着色器与片段着色器,就是为了着色。

阅读全文 »

Binary-Watch

发表于 2017-12-14 | 更新于: 2018-08-22

LeetCode#401 Binary Watch

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

Each LED represents a zero or one, with the least significant bit on the right.

For example, the above binary watch reads “3:25”.

阅读全文 »

【OpenGL】3-这是一个三角形2

发表于 2017-12-13 | 更新于: 2018-08-22

Shader

  这篇文章我们将简单介绍一下Shader与对Shader编程,在此将讨论OpenGL的Shader语言GLSL。

  首先推荐教程(毕竟我这里只是我自己学的经历,为防止错误的思想误导了别人,还是先摆出比较正确的教程再说)

​ 1. 着色器

​ 2. GLSL

阅读全文 »
1…424344…46
KsGin

KsGin

游戏程序员/计算机图形学/Unity/西山居搬砖

226 日志
16 标签
GitHub E-Mail Google Twitter
© 2018 KsGin @ 2018
0%