397.整數替換
給定一個正整數 n,你可以做如下操作:
1. 如果 n 是偶數,則用 n / 2
替換 n。
2. 如果 n 是奇數,則可以用 n + 1
或n - 1
替換 n。
n 變為 1 所需的最小替換次數是多少?
示例 1:
輸入:8 輸出:3 解釋:8 -> 4 -> 2 -> 1
示例 2:
輸入:7 輸出:4 解釋:7 -> 8 -> 4 -> 2 -> 1或7 -> 6 -> 3 -> 2 -> 1
class Solution {
public:
int integerReplacement(int n) {
if (n == 1) return 0;
if (n % 2 == 0) return 1 + integerReplacement(n / 2);
else {
long long t = n;
return 2 + min(integerReplacement((t + 1) / 2), integerReplacement((t - 1) / 2));
}
}
};
相關推薦
397.整數替換
給定一個正整數 n,你可以做如下操作: 1. 如果 n 是偶數,則用 n / 2替換 n。 2. 如果 n 是奇數,則可以用 n + 1或n - 1替換 n。n 變為 1 所需的最小替換次數是多少
Leetcode:397.整數替換
給定一個正整數 n,你可以做如下操作: 1. 如果 n 是偶數,則用 n / 2替換 n。 2. 如果 n 是奇數,則可以用 n + 1或n - 1替換 n。n 變為 1 所需的最小替換次數是多少
Leetcode ---- 397. 整數替換
題目: 給定一個正整數 n,你可以做如下操作: 1. 如果 n 是偶數,則用 n / 2替換 n。 2. 如果 n 是奇數,則可以用 n + 1或n - 1替換 n。n 變為 1 所需的最小替
Leetcode 397.整數替換
整數替換 給定一個正整數 n,你可以做如下操作: 1. 如果 n 是偶數,則用 n / 2替換 n。2. 如果 n 是奇數,則可以用 n + 1或n - 1替換 n。n 變為 1 所需的最小替換次數是多少
[LeetCode] Integer Replacement 整數替換
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If n is odd, you can replace n with either n + 1
shell比較浮點數和整數
ssi 工作 示例 一個 style 朋友 shell code nbsp 今天有一個朋友忽然問我在shell中,如何比較浮點數和整數,倒是把我問的一楞,在工作中確實沒有遇到這個場景。我們也知道,在shell中數字的計算通常都會轉換成整數,比如說1.1和1會被認為是一樣
設計模式六大原則之裏氏替換原則
number -h ole 擁有 method about rect sse 程序 1 裏氏替換原則定義 Liskov於1987年提出了一個關於繼承的原則“Inheritance should ensure that any property proved about su
利用C++ string實現任意長度小數、整數之間的加法
code include spa ctype empty esp pac space temp 1 /* 2 大數的運算1--加法: 3 利用C++ string實現任意長度小數、整數之間的加法 4 作者:大大維 5 2017/5/5
數組的方法(連接,截取,刪除,插入,替換,以及封裝一個函數)
log clas span 選擇 數組遍歷 p s func 第一個 code 連接兩個數組;concat,形成一個新數組 數組1.concat(數組2,數組1) 返回值:數組 var arr1=[1,2], arr2=[3,4], arr3; arr3=arr1.con
python fileinput 模塊替換文件全局內容
python fileinputnginx.conf 文件有如下內容: server { ..................................................................... server 192.
面向對象五大原則_1.單一職責原則&2.裏氏替換原則
解決 一次 cti prot 輸入 名稱 enter wid col 單一職責原則:Single Responsibility Principle (SRP) 一個類。僅僅有一個引起它變化的原因。應該僅僅有一個職責。每個職責都是變化的一個軸線。假設一個類有一個以
int整數和bool值
數據 bool 布爾 二進制表示 blog 內容 bsp int bytes # ########################################## int 整數 ########################################## #
[python 源碼]整數對象的創建和維護
系統 十個 。。 col 為什麽 參數 結果 太多的 發現 剛開始學python時候,發現一個很迷惑的現象,一直到看了源碼後才知道了: >>> a=6 >>> b=6 >>> a is b True 想用同樣的參數初
ASP.NET MVC 替換默認的基架模板
不想 esp black 函數返回 als work 相同 bae detail 在學習Asp.net Mvc中,今天第一次聽了基架,哈哈! 常用的首字母縮略詞 CRUD 恰當地傳達了根據數據存儲編寫例程創建、檢索、更新和刪除操作的普通任務。Microsoft 提供由
替換空格
面試 特殊字符 .com 字符串長度 快的 必須 %20 i++ copy 題目:請實現一個函數,將一個字符串中的空格替換成“%20”。例如,當字符串為We Are Happy.則經過替換之後的字符串為We%20Are%20Happy。 解析:如果從前往後替換字符串中的空格
shell腳本-比較兩個整數大小
shell開發shell腳本分別實現以腳本傳參以及read讀入的方式比較2個整數大小。用條件表達式(禁止if)進行判斷並以屏幕輸出的方式提醒用戶比較結果。註意:一共是開發2個腳本。當用腳本傳參以及read讀入的方式需要對變量是否為數字、並且傳參個數不對給予提示。read讀入方式#!/bin/bash read
大整數加法與減法
esp bre pre 大整數加法 對齊 style cstring 小數 nbsp 模擬運算 用數組逆向保存大數每一位 加法直接從尾向前相加(尾部已對齊) 註意進位 減法類似 確保大數減小數 不夠減了向前減一 需要註意符號的有無問題 1 #include <i
二十四進制編碼串轉換為32位無符號整數(C語言實現)
bool while open 參數錯誤 hint div 第一個字符 bsp opened typedef int BOOL; #define TRUE 1; #define FALSE 0; #define UINT_MAX 0xffffffff
Jquery替換內容
min con 愛的 repl html doc interval tex blog <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-
mysql中使用instr替換like
方式 too 替換 bar like strong tools 內部函數 name 使用內部函數instr,可代替傳統的like方式查詢,並且速度更快。 instr函數,第一個參數是字段,第二個參數是要查詢的串,返回串的位置,第一個是1,如果沒找到就