869. 重新排序得到 2 的冪

869. 重新排序得到 2 的冪

給定正整數 N ,我們按任何順序(包括原始順序)將數字重新排序,注意其前導數字不能為零。

如果我們可以通過上述方式得到 2 的冪,返回 true;否則,返回 false。

  • 示例 1:

輸入:1
輸出:true

  • 示例 2:

輸入:10
輸出:false

  • 示例 3:

輸入:16
輸出:true

  • 示例 4:

輸入:24
輸出:false

  • 示例 5:

輸入:46
輸出:true

解題思路

嘗試數字的所有排列方式,檢查每種排列方式是否能組成2的冪次

代碼

class Solution {Set<Integer> tar=new HashSet<>();public boolean reorderedPowerOf2(int n) {for (int i=0;i<31;i++){tar.add(1<<i);}String s=""+n;return dfsReorderedPowerOf2(s,new boolean[s.length()],0,new StringBuilder());}public boolean dfsReorderedPowerOf2(String s,boolean[] m,int cur,StringBuilder sb) {if (cur==s.length()){if (tar.contains(Integer.parseInt(sb.toString())))return true;return false;}boolean f=false;for (int i = 0; i < m.length; i++) {if (m[i]||cur==0&&s.charAt(i)=='0')continue;m[i]=true;sb.append(s.charAt(i));f|=dfsReorderedPowerOf2(s,m,cur+1,sb);sb.deleteCharAt(sb.length()-1);m[i]=false;}return f;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389907.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389907.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389907.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

org.apache.maven.archiver.MavenArchiver.getManifest

eclipse導入新的maven項目時&#xff0c;pom.xml第一行報錯&#xff1a; org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration) 解決辦法&#xff1a; help -> Install New…

殺進程常用命令

殺進程命令pkill 進程名killall 進程名 # 平緩kill -HUP pid # 平緩kill -USR2 pidkill pid &#xff08;-9 不要使用&#xff09;轉載于:https://www.cnblogs.com/jmaly/p/9492406.html

CertUtil.exe被利用來下載惡意軟件

1、前言 經過國外文章信息&#xff0c;CertUtil.exe下載惡意軟件的樣本。 2、實現原理 Windows有一個名為CertUtil的內置程序&#xff0c;可用于在Windows中管理證書。使用此程序可以在Windows中安裝&#xff0c;備份&#xff0c;刪除&#xff0c;管理和執行與證書和證書存儲相…

335. 路徑交叉

335. 路徑交叉 給你一個整數數組 distance 。 從 X-Y 平面上的點 (0,0) 開始&#xff0c;先向北移動 distance[0] 米&#xff0c;然后向西移動 distance[1] 米&#xff0c;向南移動 distance[2] 米&#xff0c;向東移動 distance[3] 米&#xff0c;持續移動。也就是說&#x…

回歸分析假設_回歸分析假設的最簡單指南

回歸分析假設The Linear Regression is the simplest non-trivial relationship. The biggest mistake one can make is to perform a regression analysis that violates one of its assumptions! So, it is important to consider these assumptions before applying regress…

Spring Aop之Advisor解析

2019獨角獸企業重金招聘Python工程師標準>>> 在上文Spring Aop之Target Source詳解中&#xff0c;我們講解了Spring是如何通過封裝Target Source來達到對最終獲取的目標bean進行封裝的目的。其中我們講解到&#xff0c;Spring Aop對目標bean進行代理是通過Annotatio…

react事件處理函數中綁定this的bind()函數

問題引入 import React, { Component } from react; import {Text,View } from react-native;export default class App extends Component<Props> {constructor(props){super(props)this.state{times:0}this.timePlusthis.timePlus.bind(this);}timePlus(){let timethis…

301. 刪除無效的括號

301. 刪除無效的括號 給你一個由若干括號和字母組成的字符串 s &#xff0c;刪除最小數量的無效括號&#xff0c;使得輸入的字符串有效。 返回所有可能的結果。答案可以按 任意順序 返回。 示例 1&#xff1a; 輸入&#xff1a;s “()())()” 輸出&#xff1a;["(())…

為什么隨機性是信息

用位思考 (Thinking in terms of Bits) Imagine you want to send outcomes of 3 coin flips to your friends house. Your friend knows that you want to send him those messages but all he can do is get the answer of Yes/No questions arranged by him. Lets assume th…

Chrome無法播放m3u8格式的直播視頻流的問題解決

出國&#xff0c;然后安裝這個插件即可&#xff1a;Native HLS Playback https://chrome.google.com/webstore/detail/native-hls-playback/emnphkkblegpebimobpbekeedfgemhof?hlzh-CN轉載于:https://www.cnblogs.com/EasonJim/p/8737001.html

大數據相關從業_如何在組織中以數據從業者的身份閃耀

大數據相關從業Build bridges, keep the maths under your hat and focus on serving.架起橋梁&#xff0c;將數學放在腦海中&#xff0c;并專注于服務。 通過協作而不是通過孤立的孤島來交付出色的數據工作。 (Deliver great data work through collaboration not through co…

暑假周總結六

本周開始了做網站的商品展示和商品查詢的功能&#xff0c;基本功能已完成了。平均每天花4到5個小時進行學習和編碼 這周學習了lucene分詞器&#xff0c;但是雖然學了一些這些方面的東西&#xff0c;但是查詢的時候效果還是不行&#xff0c;還是繼續學習 一些更好處理關鍵字的方…

Django進階之中間件

中間件簡介 在http請求 到達視圖函數之前 和視圖函數return之后&#xff0c;django會根據自己的規則在合適的時機執行中間件中相應的方法。 中間件的執行流程 1、執行完所有的request方法 到達視圖函數。 2、執行中間件的其他方法 2、經過所有response方法 返回客戶端。 注意…

漢諾塔遞歸算法進階_進階python 1遞歸

漢諾塔遞歸算法進階When something is specified in terms of itself, it is called recursion. The recursion gives us a new idea of how to solve a kind of problem and this gives us insights into the nature of computation. Basically, many of computational artifa…

500. 鍵盤行

500. 鍵盤行 給你一個字符串數組 words &#xff0c;只返回可以使用在 美式鍵盤 同一行的字母打印出來的單詞。鍵盤如下圖所示。 美式鍵盤 中&#xff1a; 第一行由字符 “qwertyuiop” 組成。 第二行由字符 “asdfghjkl” 組成。 第三行由字符 “zxcvbnm” 組成。 示例 1&a…

windows 停止nginx

1、查找進程 tasklist | findstr nginx2、殺死進程 taskkill /pid 6508 /F3、一次殺死多個進程taskkill /pid 6508 /pid 16048 /f轉載于:https://blog.51cto.com/dressame/2161759

SpringBoot返回json和xml

有些情況接口需要返回的是xml數據&#xff0c;在springboot中并不需要每次都轉換一下數據格式&#xff0c;只需做一些微調整即可。 新建一個springboot項目&#xff0c;加入依賴jackson-dataformat-xml&#xff0c;pom文件代碼如下&#xff1a; <?xml version"1.0&quo…

575. 分糖果

575. 分糖果 給定一個偶數長度的數組&#xff0c;其中不同的數字代表著不同種類的糖果&#xff0c;每一個數字代表一個糖果。你需要把這些糖果平均分給一個弟弟和一個妹妹。返回妹妹可以獲得的最大糖果的種類數。 示例 1:輸入: candies [1,1,2,2,3,3] 輸出: 3 解析: 一共有三…

如何開啟并配置CITRIX Xenserver的SNMP服務

以下博文轉載至虛擬人生Citrix Xenserver使用標準的NET-SNMP協議&#xff0c;關于NET-SNMP請參考www.net-snmp.org. Xenserver并沒有自己的MIB庫.Xenserver默認是禁止SNMP服務且并沒有開啟SNMP服務使用的端口,通過以下方式開啟并配置SNMP服務&#xff1a;1.編輯Xenserver的/etc…

orange 數據分析_使用Orange GUI的放置結果數據分析

orange 數據分析Objective : Analysing of several factors influencing the recruitment of students and extracting information through plots.目的&#xff1a;分析影響學生招生和通過情節提取信息的幾個因素。 Description : The following analysis presents the diffe…