集合
一、概念
集合结构
- 集合通常是由一组无序的,不能重复的元素构成。
- 集合中的元素不允许重复。
- 特殊的数组:
- 元素==没有顺序,不能重复==。
- 没有顺序意味着==不能通过下标访问==;
- ES6 中的 Set结构 就是一种集合结构
二、属性和方法
集合的方法:
- add():向集合中添加一个新的元素
- remove():从集合中移除一个元素
- has():检查元素是否存在于集合中,若存在,返回true,否则返回false
- clear():移除集合中的所有元素
- size():返回集合所包含的元素的数量
- values():返回一个包含集合中所有值的数组
- union():并集
- intersection():交集
- defference():差集
- subset():子集, A是否是B的子集
具体实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| function Set() {
this.items = {};
Set.prototype.add = function (value) { if (this.has(value)) { return false; } this.items[value] = value; return true; }
Set.prototype.remove = function (value) {
if(!this.has(value)){ return false; }
delete this.items[value]; return true; }
Set.prototype.has = function (value) { return this.items.hasOwnProperty(value); }
Set.prototype.clear = function () { this.items = {}; }
Set.prototype.size = function () { return Object.keys(this.items).length; }
Set.prototype.values = function () { return Object.keys(this.items); }
Set.prototype.union = function(otherSet) {
const unionSet = new Set();
const values = this.values(); for (let i = 0; i < values.length; i++) { unionSet.add(values[i]); }
const values2 = otherSet.values(); for (let i = 0; i < values2.length; i++) { unionSet.add(values2[i]); }
return unionSet; }
Set.prototype.intersection = function(otherSet) {
const intersectionSet = new Set();
const values = this.values(); for (let i = 0; i < values.length; i++) { const item_a = values[i]
if (otherSet.has(item_a)) { intersectionSet.add(item_a); } }
return intersectionSet; }
Set.prototype.defference = function(otherSet) {
const defferenceSet = new Set();
const values = this.values(); for (let i = 0; i < values.length; i++) { const item_a = values[i]
if (!otherSet.has(item_a)) { defferenceSet.add(item_a); } }
return defferenceSet; }
Set.prototype.subset = function(otherSet) {
const subSet = new Set();
const values = this.values(); for (let i = 0; i < values.length; i++) { const item = values[i];
if(!otherSet.has(item)) { return false; } }
return true; } }
|
测试用例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| const set = new Set();
set.add('aa'); set.add('bb'); set.add('cc'); set.add('dd');
set.remove('dd');
set.values();
set.has('aa'); set.has('dd');
set.size();
const otherSet = new Set(); otherSet.add('cc'); otherSet.add('dd');
console.log('集合A:', set.values()); console.log('集合B:', otherSet.values());
const unionSet = set.union(otherSet); console.log('并集:', unionSet.values());
const intersectionSet = set.intersection(otherSet); console.log('交集:', intersectionSet.values());
const defferenceSet = set.defference(otherSet); console.log('差集:', defferenceSet.values());
console.log('*************** 子集 *****************')
const setC = new Set(); setC.add('aa'); setC.add('bb'); setC.add('cc'); setC.add('dd'); console.log('集合C:', setC.values());
const subSet1 = set.subset(otherSet); console.log('A是B的子集:', subSet1);
const subSet2 = set.subset(setC); console.log('A是C的子集:', subSet2);
|
-
Post title: 集合
-
Post author: Chao
-
Create time: 2019-11-11 14:45:00
-
Post link: 2019/11/11/集合Set/
-
Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
$tools-item-width = 2.2rem
$tools-item-font-size = 1.1rem
$tools-item-border-radius = 0.1rem
.side-tools-container {
position relative
.tools-item {
width $tools-item-width
height $tools-item-width
margin-bottom 0.2rem
color var(--text-color-3)
font-size $tools-item-font-size
background var(--background-color-1)
border-right none
border-radius $tools-item-border-radius
box-shadow 0.1rem 0.1rem 0.2rem var(--shadow-color)
cursor pointer
i {
color var(--text-color-3)
}
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
i {
color var(--background-color-1)
}
}
+keep-tablet() {
width $tools-item-width * 0.9
height $tools-item-width * 0.9
margin-bottom 0.2rem
font-size $tools-item-font-size * 0.9
}
&.rss {
a {
width 100%
height 100%
border-radius $tools-item-border-radius
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
}
}
}
}
.side-tools-list {
transform translateX(100%)
opacity 0
transition-t("transform, opacity", "0, 0", "0.2, 0.2", "linear, linear")
&.show {
transform translateX(0)
opacity 1
}
}
.exposed-tools-list {
if (hexo-config('style.scroll.percent') == true) {
.tool-scroll-to-top {
display none
&.show {
display flex
}
&:hover {
.percent {
display none
}
.arrow-up {
display flex
}
}
.arrow-up {
display none
}
.percent {
display flex
font-size 1rem
}
}
}
}
}
$icon-size = 1.2rem
$search-header-height = 3rem
.search-pop-overlay {
position fixed
top 0
left 0
z-index $z-index-8
display flex
width 100%
height 100%
background rgba(0, 0, 0, 0)
visibility hidden
transition-t("visibility, background", "0, 0", "0.3, 0.3", "ease, ease")
&.active {
background rgba(0, 0, 0, 0.35)
visibility visible
.search-popup {
transform scale(1)
}
}
.search-popup {
z-index $z-index-6
width 70%
height 80%
margin auto
background var(--background-color-1)
border-radius 0.4rem
transform scale(0)
transition-t("transform", "0", "0.3", "ease")
+keep-tablet() {
width 80%
}
+keep-mobile() {
width 90%
}
.search-header {
display flex
align-items center
height $search-header-height
padding 0 1rem
background var(--text-color-6)
border-top-left-radius 0.2rem
border-top-right-radius 0.2rem
.search-input-field-pre {
margin-right 0.2rem
color var(--text-color-3)
font-size 1.3rem
cursor pointer
}
.search-input-container {
flex-grow 1
padding 0.2rem
.search-input {
width 100%
color var(--text-color-3)
font-size 1.2rem
background transparent
border 0
outline 0
&::-webkit-search-cancel-button {
display none
}
&::-webkit-input-placeholder {
color var(--text-color-4)
font-size 1rem
}
}
}
.close-popup-btn {
color var(--text-color-3)
font-size $icon-size
cursor pointer
&:hover {
color var(--text-color-1)
}
}
}
#search-result {
position relative
display flex
box-sizing border-box
height 'calc(100% - %s)' % $search-header-height
padding 0.3rem 1.5rem
overflow auto
.search-result-list {
width 100%
height 100%
font-size 1rem
li {
box-sizing border-box
margin 0.8rem 0
padding 0.8rem 0
border-bottom 0.1rem dashed var(--border-color)
&:last-child {
border-bottom none
}
.search-result-title {
position relative
display flex
align-items center
margin-bottom 0.8rem
padding-left 1rem
font-weight bold
&::after {
position absolute
top 50%
left 0
width 0.4rem
height 0.4rem
background var(--text-color-3)
border-radius 50%
transform translateY(-50%)
content ''
}
}
.search-result {
margin 0
padding-left 1rem
line-height 2rem
word-wrap break-word
}
a {
&:hover {
color var(--text-color-3)
}
}
.search-keyword {
color var(--primary-color)
font-weight bold
border-bottom 0.1rem dashed var(--primary-color)
}
}
}
#no-result {
margin auto
color var(--text-color-4)
}
}
}
}