mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 1461、1721
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func hasAllCodes(s string, k int) bool {
|
||||
need := int(math.Pow(2.0, float64(k)))
|
||||
visited, mask, curr := make([]bool, need), (1<<k)-1, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
curr = ((curr << 1) | int(s[i]-'0')) & mask
|
||||
if i >= k-1 { // mask 有效位达到了 k 位
|
||||
if !visited[curr] {
|
||||
need--
|
||||
visited[curr] = true
|
||||
if need == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1461 struct {
|
||||
para1461
|
||||
ans1461
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1461 struct {
|
||||
s string
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1461 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem1461(t *testing.T) {
|
||||
|
||||
qs := []question1461{
|
||||
|
||||
{
|
||||
para1461{"00110110", 2},
|
||||
ans1461{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1461{"00110", 2},
|
||||
ans1461{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1461{"0110", 1},
|
||||
ans1461{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1461{"0110", 2},
|
||||
ans1461{false},
|
||||
},
|
||||
|
||||
{
|
||||
para1461{"0000000001011100", 4},
|
||||
ans1461{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1461------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1461, q.para1461
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, hasAllCodes(p.s, p.k))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
# [1461. Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary string `s` and an integer `k`.
|
||||
|
||||
Return *True* if every binary code of length `k` is a substring of `s`. Otherwise, return *False*.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "00110110", k = 2
|
||||
Output: true
|
||||
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "00110", k = 2
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: s = "0110", k = 1
|
||||
Output: true
|
||||
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: s = "0110", k = 2
|
||||
Output: false
|
||||
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.
|
||||
```
|
||||
|
||||
**Example 5:**
|
||||
|
||||
```
|
||||
Input: s = "0000000001011100", k = 4
|
||||
Output: false
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= s.length <= 5 * 10^5`
|
||||
- `s` consists of 0's and 1's only.
|
||||
- `1 <= k <= 20`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个二进制字符串 `s` 和一个整数 `k` 。如果所有长度为 `k` 的二进制字符串都是 `s` 的子串,请返回 `True` ,否则请返回 `False` 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 构造一个 `mask` 遮罩,依次划过整个二进制字符串,每次滑动即取出遮罩遮住的 `k` 位二进制字符。可以用 `map` 存储不同的二进制转换成的十进制数,最后判断 `len(map)` 是否等于 `k` 即可。但是用 `map` 存比较慢,此处换成 `bool` 数组。先构造一个长度为 `k` 的数组,然后每次通过 `mask` 更新这个 `bool` 数组对应十进制的 `bool` 值,并且记录剩余还缺几个二进制数。等剩余的等于 0 的时候,说明所有二进制字符串都出现了,直接输出 `true`,否则循环完以后输出 `false`。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func hasAllCodes(s string, k int) bool {
|
||||
need := int(math.Pow(2.0, float64(k)))
|
||||
visited, mask, curr := make([]bool, need), (1<<k)-1, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
curr = ((curr << 1) | int(s[i]-'0')) & mask
|
||||
if i >= k-1 { // mask 有效位达到了 k 位
|
||||
if !visited[curr] {
|
||||
need--
|
||||
visited[curr] = true
|
||||
if need == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
```
|
@ -0,0 +1,36 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
func swapNodes(head *ListNode, k int) *ListNode {
|
||||
count := 1
|
||||
var a, b *ListNode
|
||||
for node := head; node != nil; node = node.Next {
|
||||
if count == k {
|
||||
a = node
|
||||
}
|
||||
count++
|
||||
}
|
||||
length := count
|
||||
count = 1
|
||||
for node := head; node != nil; node = node.Next {
|
||||
if count == length-k {
|
||||
b = node
|
||||
}
|
||||
count++
|
||||
}
|
||||
a.Val, b.Val = b.Val, a.Val
|
||||
return head
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question2 struct {
|
||||
para2
|
||||
ans2
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para2 struct {
|
||||
head []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans2 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem2(t *testing.T) {
|
||||
|
||||
qs := []question2{
|
||||
|
||||
{
|
||||
para2{[]int{1, 2, 3, 4, 5}, 2},
|
||||
ans2{[]int{1, 4, 3, 2, 5}},
|
||||
},
|
||||
|
||||
{
|
||||
para2{[]int{7, 9, 6, 6, 7, 8, 3, 0, 9, 5}, 5},
|
||||
ans2{[]int{7, 9, 6, 6, 8, 7, 3, 0, 9, 5}},
|
||||
},
|
||||
|
||||
{
|
||||
para2{[]int{1}, 1},
|
||||
ans2{[]int{1}},
|
||||
},
|
||||
|
||||
{
|
||||
para2{[]int{1, 2}, 1},
|
||||
ans2{[]int{2, 1}},
|
||||
},
|
||||
|
||||
{
|
||||
para2{[]int{1, 2, 3}, 2},
|
||||
ans2{[]int{1, 2, 3}},
|
||||
},
|
||||
// 如需多个测试,可以复制上方元素。
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 2------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans2, q.para2
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, structures.List2Ints(swapNodes(structures.Ints2List(p.head), p.k)))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
100
leetcode/1721.Swapping-Nodes-in-a-Linked-List/README.md
Normal file
100
leetcode/1721.Swapping-Nodes-in-a-Linked-List/README.md
Normal file
@ -0,0 +1,100 @@
|
||||
# [1721. Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given the `head` of a linked list, and an integer `k`.
|
||||
|
||||
Return *the head of the linked list after **swapping** the values of the* `kth` *node from the beginning and the* `kth` *node from the end (the list is **1-indexed**).*
|
||||
|
||||
**Example 1:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: head = [1,2,3,4,5], k = 2
|
||||
Output: [1,4,3,2,5]
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
|
||||
Output: [7,9,6,6,8,7,3,0,9,5]
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: head = [1], k = 1
|
||||
Output: [1]
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: head = [1,2], k = 1
|
||||
Output: [2,1]
|
||||
```
|
||||
|
||||
**Example 5:**
|
||||
|
||||
```
|
||||
Input: head = [1,2,3], k = 2
|
||||
Output: [1,2,3]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- The number of nodes in the list is `n`.
|
||||
- `1 <= k <= n <= 10^5`
|
||||
- `0 <= Node.val <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你链表的头节点 `head` 和一个整数 `k` 。**交换** 链表正数第 `k` 个节点和倒数第 `k` 个节点的值后,返回链表的头节点(链表 **从 1 开始索引**)。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这道题虽然是 medium,但是实际非常简单。题目要求链表中 2 个节点的值,无非是先找到这 2 个节点,然后再交换即可。链表查询节点需要 O(n),2 次循环找到对应的 2 个节点,交换值即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// ListNode define
|
||||
type ListNode = structures.ListNode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
func swapNodes(head *ListNode, k int) *ListNode {
|
||||
count := 1
|
||||
var a, b *ListNode
|
||||
for node := head; node != nil; node = node.Next {
|
||||
if count == k {
|
||||
a = node
|
||||
}
|
||||
count++
|
||||
}
|
||||
length := count
|
||||
count = 1
|
||||
for node := head; node != nil; node = node.Next {
|
||||
if count == length-k {
|
||||
b = node
|
||||
}
|
||||
count++
|
||||
}
|
||||
a.Val, b.Val = b.Val, a.Val
|
||||
return head
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user