Add solution 869

This commit is contained in:
YDZ
2021-03-22 00:22:28 +08:00
parent fe7d9845bd
commit 613fa9aa92
25 changed files with 597 additions and 291 deletions

View File

@ -0,0 +1,32 @@
package leetcode
import "fmt"
func reorderedPowerOf2(n int) bool {
sample, i := fmt.Sprintf("%v", n), 1
for len(fmt.Sprintf("%v", i)) <= len(sample) {
t := fmt.Sprintf("%v", i)
if len(t) == len(sample) && isSame(t, sample) {
return true
}
i = i << 1
}
return false
}
func isSame(t, s string) bool {
m := make(map[rune]int)
for _, v := range t {
m[v]++
}
for _, v := range s {
m[v]--
if m[v] < 0 {
return false
}
if m[v] == 0 {
delete(m, v)
}
}
return len(m) == 0
}

View File

@ -0,0 +1,72 @@
package leetcode
import (
"fmt"
"testing"
)
type question869 struct {
para869
ans869
}
// para 是参数
// one 代表第一个参数
type para869 struct {
n int
}
// ans 是答案
// one 代表第一个答案
type ans869 struct {
one bool
}
func Test_Problem869(t *testing.T) {
qs := []question869{
{
para869{1},
ans869{true},
},
{
para869{10},
ans869{false},
},
{
para869{16},
ans869{true},
},
{
para869{24},
ans869{false},
},
{
para869{46},
ans869{true},
},
{
para869{100},
ans869{false},
},
{
para869{123453242},
ans869{false},
},
}
fmt.Printf("------------------------Leetcode Problem 869------------------------\n")
for _, q := range qs {
_, p := q.ans869, q.para869
fmt.Printf("【input】:%v 【output】:%v\n", p, reorderedPowerOf2(p.n))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,93 @@
# [869. Reordered Power of 2](https://leetcode.com/problems/reordered-power-of-2/)
## 题目
Starting with a positive integer `N`, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return `true` if and only if we can do this in a way such that the resulting number is a power of 2.
**Example 1:**
```
Input:1
Output:true
```
**Example 2:**
```
Input:10
Output:false
```
**Example 3:**
```
Input:16
Output:true
```
**Example 4:**
```
Input:24
Output:false
```
**Example 5:**
```
Input:46
Output:true
```
**Note:**
1. `1 <= N <= 10^9`
## 题目大意
给定正整数 N 我们按任何顺序包括原始顺序将数字重新排序注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true否则返回 false。
## 解题思路
- 将整数每个位上的所有排列看成字符串,那么题目转换为判断这些字符串是否和 2 的幂的字符串是否一致。判断的方法有很多种,笔者这里判断借助了一个 `map`。两个不同排列的字符串要相等,所有字符出现的频次必定一样。利用一个 `map` 统计它们各自字符的频次,最终都一致,则判定这两个字符串是满足题意的。
- 此题数据量比较小,在 `[1,10^9]` 这个区间内2 的幂只有 30 几个,所以最终要判断的字符串就是这 30 几个。笔者这里没有打表了,采用更加一般的做法。数据量更大,此解法代码也能通过。
## 代码
```go
package leetcode
import "fmt"
func reorderedPowerOf2(n int) bool {
sample, i := fmt.Sprintf("%v", n), 1
for len(fmt.Sprintf("%v", i)) <= len(sample) {
t := fmt.Sprintf("%v", i)
if len(t) == len(sample) && isSame(t, sample) {
return true
}
i = i << 1
}
return false
}
func isSame(t, s string) bool {
m := make(map[rune]int)
for _, v := range t {
m[v]++
}
for _, v := range s {
m[v]--
if m[v] < 0 {
return false
}
if m[v] == 0 {
delete(m, v)
}
}
return len(m) == 0
}
```