位运算基础——找出数组中唯一一个只有一个的数

简单模式:数组中只有一个数出现一次,其他均出现两次,请找出这个数。

1
2
3
4
5
6
7
8
//原理:a ^ a = 0   a ^ 0 = a
int FindOnlySum(vector<int>& nums){
int result = 0;
for(auto& num : nums){
result ^= num;
}
return result;
}

进阶模式:数组中只有一个数出现一次,其他均出现三次,请找出这个数。(题目来自力扣137)

例:
【1,2,3,2,4,4,3,4,2,3】
0001
0010
0011
0010
0100
0100
0011
0100
0010
0011
++++
0364
%%%%3
0001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//
int FindOnlySum(vector<int>& nums){
int ans = 0;
for (int i = 0; i < 32; ++i) {
int total = 0;
for (int num: nums) {
total += ((num >> i) & 1);//(num >> i) & 1 -> num左移i位,然后与1与 == 取num的第i位
}
if (total % 3) { //total % 3 = 0 / 1
ans |= (1 << i); // ans | (1 << i) -> 1右移i位,与ans或 == 将ans的第i位变成1
}
}
return ans;
}

算法改进

第i位(ai,bi)初始(00)连续经过三个零或三个1又变回(00)

(00)->(01)->(10)->(00)

(ai,bi) xi 新(ai,bi)
00 0 00
00 1 01
01 0 01
01 1 10
10 0 10
10 1 00

a = (~a & b & x) | (a & ~b & ~x)
b = ~a & (b ^ x)

最后结果:ai=0 ,bi=0/1,即返回b即可。

1
2
3
4
5
6
7
int FindOnlySum(vector<int>& nums) {
int a = 0, b = 0;
for (int num: nums) {
tie(a, b) = pair{(~a & b & num) | (a & ~b & ~num), ~a & (b ^ num)};
}
return b;
}

进一步改进–同时计算改为分步计算

因为bi计算更简单,所以我们先计算bi,再用新bi计算ai。
(00)->(01) => (00)->(01)->(01)

(ai,bi) xi 新bi
00 0 0
00 1 1
01 0 1
01 1 0
10 0 0
10 1 0
(新ai,bi) xi 新ai
00 0 0
01 1 0
01 0 0
00 1 1
10 0 1
10 1 0

b = ~a & (b ˆ x)
a = ~b & (a ˆ x)

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int FindOnlySum(vector<int>& nums) {
int a = 0, b = 0;
for (int num: nums) {
b = ~a & (b ^ num);
a = ~b & (a ^ num);
}
return b;
}
};

vue学习之路--基础知识

组合式vue3

1.reative,ref

1
2
3
4
5
6
7
8
9
10
11
<script setup>
import { reactive, ref } from 'vue'

const counter = reactive({ count: 0 })//reactive()只适用于对象 (包括数组和内置类型,如 Map 和 Set)
const message = ref('Hello World!')//ref()则可以接受任何值类型。ref 会返回一个包裹对象,并在 .value 属性下暴露内部值。
</script>

<template>
<h1>{{ message }}</h1>
<p>Count is: {{ counter.count }}</p>
</template>

2.Attrictive绑定 v-bind:id => :id

v-bind:id=”dynamicId” 可简写成 :id=”dynamicId”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script setup>
import { ref } from 'vue'

const titleClass = ref('title')
</script>

<template>
<h1 :class="titleClass">Make me red</h1>
</template>

<style>
.title {
color: red;
}
</style>

3.事件监听 v-on:click => @click

v-on:click=”increment”> 简写成 @click=”increment”>

1
2
3
4
5
6
7
8
9
10
11
12
13
<script setup>
import { ref } from 'vue'

const count = ref(0)

function increment() {
count.value++
}
</script>

<template>
<button @click="increment">count is: {{ count }}</button>
</template>
1
2
3
4
5
6
7
8
9
10
<script setup>
import { ref } from 'vue'

const count = ref(0)

</script>

<template>
<button @click="count++">count is: {{ count }}</button>
</template>

4.表单绑定 v-bind:id = “text” 和 v-on:input = “onInput” => v-model = “text”

v-bind 和 v-on 来在表单的输入元素上创建双向绑定,为了简化双向绑定,Vue 提供了一个 v-model 指令,它实际上是语法糖

1
2
3
4
5
6
7
8

<input :value="text" @input="onInput">

function onInput(e) {
// v-on 处理函数会接收原生 DOM 事件
// 作为其参数。
text.value = e.target.value
}
1
<input v-model="text">

5.条件渲染 v-if和v-else

1
2
<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no 😢</h1>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script setup>
import { ref } from 'vue'

const awesome = ref(true)

function toggle() {
awesome.value = !awesome.value
}
</script>

<template>
<button @click="toggle">toggle</button>
<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no 😢</h1>
</template>

6.列表渲染 v-for

1
2
3
4
5
<ul>
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
</li>
</ul>
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
<script setup>
import { ref } from 'vue'

// 给每个 todo 对象一个唯一的 id
let id = 0

const newTodo = ref('')
const todos = ref([
{ id: id++, text: 'Learn HTML' },
{ id: id++, text: 'Learn JavaScript' },
{ id: id++, text: 'Learn Vue' }
])

function addTodo() {
todos.value.push({ id: id++, text: newTodo.value })
newTodo.value = ''
}

function removeTodo(todo) {
todos.value = todos.value.filter((t) => t !== todo)
}
</script>

<template>
<form @submit.prevent="addTodo">
<input v-model="newTodo">
<button>Add Todo</button>
</form>
<ul>
//使用了 filter 方法来过滤数组 todos.value 中的元素。
//通过匿名箭头函数 (t) => t !== todo,它保留了与 todo 不相等的元素,并返回一个过滤后的新数组。
<li v-for="todo in todos" :key="todo.id">
{{ todo.text }}
<button @click="removeTodo(todo)">X</button>
</li>
</ul>
</template>

7.计算属性

介绍一个新 API:computed()。它可以让我们创建一个计算属性 ref,这个 ref 会动态地根据其他响应式数据源来计算其 .value

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
<script setup>
import { ref, computed } from 'vue'

let id = 0

const newTodo = ref('')
const hideCompleted = ref(false)
const todos = ref([
{ id: id++, text: 'Learn HTML', done: true },
{ id: id++, text: 'Learn JavaScript', done: true },
{ id: id++, text: 'Learn Vue', done: false }
])

const filteredTodos = computed(() => {
return hideCompleted.value
? todos.value.filter((t) => !t.done)
: todos.value
})

function addTodo() {
todos.value.push({ id: id++, text: newTodo.value, done: false })
newTodo.value = ''
}

function removeTodo(todo) {
todos.value = todos.value.filter((t) => t !== todo)
}
</script>

<template>
<form @submit.prevent="addTodo">
<input v-model="newTodo">
<button>Add Todo</button>
</form>
<ul>
<li v-for="todo in filteredTodos" :key="todo.id">
<input type="checkbox" v-model="todo.done">
<span :class="{ done: todo.done }">{{ todo.text }}</span>
<button @click="removeTodo(todo)">X</button>
</li>
</ul>
<button @click="hideCompleted = !hideCompleted">
{{ hideCompleted ? 'Show all' : 'Hide completed' }}
</button>
</template>

<style>
.done {
text-decoration: line-through;
}
</style>

vue学习之路--其他知识

在script标签中写js代码,或者使用src引入js文件时,默认不能使用module形式,即不能使用import导入文件,但是我们可以再script标签上加上type=module属性来改变方式。

1
2
3
4
5
6
7
//module.js
export default function test(){
return 'test'
}
// index.js
import test from './module.js';
console.log(test())
1
2
3
4
5
6
7
8
9
10
11
// index.html
<body>
// 方法 1 : 引入module.js,然后在script标签里面调用
<script type="module">
import test from './module.js';
console.log(test())
</script>

// 方法 2 : 直接引入index.js,使用src引入
<script type="module" src="./index.js"></script>
</body>

鼠标悬停效果

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
<template>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
<HelloWorld msg="Hello" />
</template>

<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
//hover : 鼠标悬停效果
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>

如何使用 makedown 编写个人博客

1.标题

1
2
3
4
5
6
7
8
9
10
11
一级标题
=======
二级标题
--------
或者
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题

2.字体

1
2
3
4
5
6
*斜体文本*
_斜体文本_
**粗体文本**
__粗体文本__
***粗斜体文本***
___粗斜体文本___

3.一些特殊线

1
2
3
4
5
6
7
8
9
10
11
12
分割线
***
* * *
*****
- - -
----------

删除线
~~BAIDU.COM~~

下划线
<u>带下划线文本</u>

4.脚注

1
2
3
4
[^要注明的文本]
如下:
创建脚注格式类似这样 [^bilibili]。
[^bilibili]: B站

创建脚注格式类似这样 ^bilibili

5.列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* 第一项
* 第二项
* 第三项

+ 第一项
+ 第二项
+ 第三项

- 第一项
- 第二项
- 第三项

1. 第一项
2. 第二项
3. 第三项

1. 第一项:
- 第一项嵌套的第一个元素
- 第一项嵌套的第二个元素
2. 第二项:
- 第二项嵌套的第一个元素
- 第二项嵌套的第二个元素

6.区块

1
2
>后加空格表示区块引用
可与列表相互嵌套

7.代码

1
2
3
4
5
6
7
8
9
10
如果是段落上的一个函数或片段的代码可以用反引号把它包起来(`),例如:
`printf()` 函数

代码区块使用4个空格或者一个制表符(tab键),例如:
<c++
int main(){
return 0;
}

也可以使用三个`包裹代码
<c++
int main(){
    return 0;
}
1
2
3
int main(){
return 0;
}

8.链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[链接名称](链接地址)
如:[bilibili](https://www.bilibili.com/)
或者
<链接地址>
如:<https://www.bilibili.com/>

高级链接:
这个链接用 1 作为网址变量 [Google][1]
这个链接用 bili 作为网址变量 [B站][bili]
然后在文档的结尾为变量赋值(网址)
[1]: http://www.google.com/
[bili]: https://www.bilibili.com/

这个链接用 1 作为网址变量 [Google][1]
这个链接用 bili 作为网址变量 [B站][bili]
然后在文档的结尾为变量赋值(网址)

[1]: http://www.google.com/
[bili]: https://www.bilibili.com/

以下是高级链接生成的结果:
这个链接用 1 作为网址变量 Google
这个链接用 bili 作为网址变量 B站
然后在文档的结尾为变量赋值(网址)
1: http://www.google.com/
bili: https://www.bilibili.com/

这个链接用 1 作为网址变量 Google
这个链接用 bili 作为网址变量 B站
然后在文档的结尾为变量赋值(网址)

9.图片

1
2
3
4
5
6
7
8
9
10
11
![alt 属性文本](图片地址)

![alt 属性文本](图片地址 "可选标题")

这个链接用 1 作为网址变量 [RUNOOB][1].
然后在文档的结尾为变量赋值(网址)

[1]: http://static.runoob.com/images/runoob-logo.png

需要指定图片的高度与宽度,可以使用html里面的img标签:
<img decoding="async" src="http://static.runoob.com/images/runoob-logo.png" width="50%">

10.表格

1
2
3
4
5
6
7
8
9
|  表头   | 表头  |
| ---- | ---- |
| 单元格 | 单元格 |
| 单元格 | 单元格 |

| 左对齐 | 右对齐 | 居中对齐 |
| :-----| ----: | :----: |
| 单元格 | 单元格 | 单元格 |
| 单元格 | 单元格 | 单元格 |

11.其他

1
2
不在 Markdown 涵盖范围之内的标签,都可以直接在文档里面用 HTML 撰写。
目前支持的 HTML 元素有:<kbd> <b> <i> <em> <sup> <sub> <br>等 ,

参考资料:菜鸟教程https://www.runoob.com/markdown/md-tutorial.html

Hello World

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment