Skip to content

Commit

Permalink
finish bit problem
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunwei committed Jan 4, 2021
1 parent c1d25e1 commit 4b68e33
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# devops-presentation
a powerpoint wbsite made by reveal-hugo
# Algorithm-presentation
a powerPoint website made by reveal-hugo
8 changes: 8 additions & 0 deletions content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,26 @@ $$

# bit

[💪去做题](/bit)

---

# 博弈论

[💪去做题](/game)

---

# 数学

[💪去做题](/math)

---

# 其他

[💪去做题](/others)

---

<h1 class="fragment fade-out" data-autoslide="1000">thank you</h1>
Expand Down
96 changes: 96 additions & 0 deletions content/bit/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
+++
title = "bit Algorithm"
description = "Algorithm presentations"
outputs = ["Reveal"]
[reveal_hugo]
margin = 0.02
transition = "slide"
transition_speed = "slow"
theme="league"
[logo]
src = "index-logo.png"
+++

<small> 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。</small>


示例 1:
```
输入: [2,2,1]
输出: 1
```

示例 2:
```
输入: [4,1,2,1,2]
输出: 4
```

---

### answer

```go
package main
func singleNumber(nums []int) int {
result := 0
for _,num :=range nums{
result ^=num
}
return result
}
```

---

<small> 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。</small>

示例 :
```
输入: [1,2,1,3,2,5]
输出: [3,5]
```

---

### answer

{{% section %}}

#### 第一步:
把所有的元素进行异或操作,最终得到一个异或值。因为是不同的两个数字,所以这个值必定不为 0;

int xor = 0;
for (int num : nums) {
xor ^= num;
}

---

第二步:
取异或值最后一个二进制位为 1 的数字作为 mask,如果是 1 则表示两个数字在这一位上不同。

int mask = xor & (-xor);

---

第三步:
通过与这个 mask 进行与操作,如果为 0 的分为一个数组,为 1 的分为另一个数组。这样就把问题降低成了:“有一个数组每个数字都出现两次,有一个数字只出现了一次,求出该数字”。对这两个子问题分别进行全异或就可以得到两个解。也就是最终的数组了。

int[] ans = new int[2];
for (int num : nums) {
if ( (num & mask) == 0) {
ans[0] ^= num;
} else {
ans[1] ^= num;
}
}

{{% /section %}}

---

#### [Start over](/#/4)


49 changes: 49 additions & 0 deletions content/game/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
+++
title = "Algorithm"
description = "Algorithm presentations"
outputs = ["Reveal"]
[reveal_hugo]
margin = 0.02
transition = "slide"
transition_speed = "slow"
theme="league"
[logo]
src = "index-logo.png"
+++

给你一个长度为n的数组nums, 其中 n>1,返回nums中除nums[i]之外其余各元素的乘积。



```
输入: [1,2,3,4]
输出: [24,12,8,6]
```

---

### answer

```go
package main
func productExceptSelf(nums []int) []int {
length := len(nums)
answer := make([]int, length)
answer[0] = 1
for i := 1; i < length; i++ {
answer[i] = nums[i-1] * answer[i-1]
}
R := 1
for i := length - 1; i >= 0; i-- {
answer[i] = answer[i] * R
R *= nums[i]
}
return answer
}

```

---
#### [Start over](/#/4)


49 changes: 49 additions & 0 deletions content/math/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
+++
title = "Algorithm"
description = "Algorithm presentations"
outputs = ["Reveal"]
[reveal_hugo]
margin = 0.02
transition = "slide"
transition_speed = "slow"
theme="league"
[logo]
src = "index-logo.png"
+++

给你一个长度为n的数组nums, 其中 n>1,返回nums中除nums[i]之外其余各元素的乘积。



```
输入: [1,2,3,4]
输出: [24,12,8,6]
```

---

### answer

```go
package main
func productExceptSelf(nums []int) []int {
length := len(nums)
answer := make([]int, length)
answer[0] = 1
for i := 1; i < length; i++ {
answer[i] = nums[i-1] * answer[i-1]
}
R := 1
for i := length - 1; i >= 0; i-- {
answer[i] = answer[i] * R
R *= nums[i]
}
return answer
}

```

---
#### [Start over](/#/4)


49 changes: 49 additions & 0 deletions content/others/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
+++
title = "Algorithm"
description = "Algorithm presentations"
outputs = ["Reveal"]
[reveal_hugo]
margin = 0.02
transition = "slide"
transition_speed = "slow"
theme="league"
[logo]
src = "index-logo.png"
+++

给你一个长度为n的数组nums, 其中 n>1,返回nums中除nums[i]之外其余各元素的乘积。



```
输入: [1,2,3,4]
输出: [24,12,8,6]
```

---

### answer

```go
package main
func productExceptSelf(nums []int) []int {
length := len(nums)
answer := make([]int, length)
answer[0] = 1
for i := 1; i < length; i++ {
answer[i] = nums[i-1] * answer[i-1]
}
R := 1
for i := length - 1; i >= 0; i-- {
answer[i] = answer[i] * R
R *= nums[i]
}
return answer
}

```

---
#### [Start over](/#/4)


0 comments on commit 4b68e33

Please sign in to comment.