發表文章

目前顯示的是 7月, 2018的文章

LeetCode第7題_Reverse Integer

圖片
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 ,  2 31  − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 思路:先嘗試取到各個字的digit,在反序去進行打印 改寫為題目對應的函式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static int reverse ( int x){ ArrayList revRes = new ArrayList(); int src = x; if (x< 0 ){ x = x*(- 1 ); } while (x> 0 ){ int digit = x% 10 ; revRes. add (digit); x/= 10 ; } int res = Integer. parseInt (revRes. toString (). replace ( "[" , "" ). replace ( "]" , "" ). replace ( ", " , ...

LeetCode第633題_Sum of Square Numbers

圖片
Given a non-negative integer  c , your task is to decide whether there're two integers  a  and  b  such that a 2  + b 2  = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: 3 Output: False 題目就是給定一個數輸入 判定是不是某兩個數的平方和 就是如此 1 -> 0^2 + 1^2 =1 -> true 2-> 1^2 + 1^2 = 2 -> true ... 3 -> x 4-> 0^2+2^2 = 4  ->true 第一版.直觀法(暴力法) 使用兩層迴圈去跑 範圍皆從0跑到該數 利用演算法【奇數之平方和必為一可開平方數】 圖像理解(正方形的邊) 所以改寫至程式進行算法優化後 原本輸入9 我的作法是給其跑兩層迴圈 i從0到9 j從0到9 ------------------------------- i=0時 j=0,j=1,j=2.......j=9給個都去跑 . . . i=9時 j=0,j=1,j=2.......j=9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the edito...

LeetCode第367題_Valid Perfect Square

圖片
Given a positive integer  num , write a function which returns True if  num  is a perfect square else False. Note:   Do not  use any built-in library function such as  sqrt . Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 注意 -2 回傳 false -1 回傳 false 0  回傳 false 1 回傳 true 2 回傳  false 4 回傳 true 16 回傳 true 然後一樣小心over flow 所以迴圈尋訪不用int 改用long 較保險 CODE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package leetcode367_validperfectsquare; import java.io.BufferedReader ; import java.io.IOException ; import java.io.InputStreamReader ; /** * * @author chous */ public class LeetCode367_ValidPerfectSquare { /** ...

LeetCode第69題_Sqrt(x)

圖片
Implement  int sqrt(int x) . Compute and return the square root of  x , where  x  is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since   the decimal part is truncated, 2 is returned. 須注意當輸入是小於0的情況 都需直接回傳-1 1 返回1 0 返回0 -1 返回-1 -2 返回-1 ... 依此類推 CODE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package leetcode69_sqrt; import java.io.BufferedReader ; import java.io.IOException ; import java.io.InputStreamRead...

LeetCode第50題_Pow(x, n)_Divide & Conquer

圖片
題目 Implement  pow( x ,  n ) , which calculates  x  raised to the power  n  (x n ). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output: 0.25000 Explanation: 2 -2 = 1/2 2 = 1/4 = 0.25 Note: -100.0 <  x  < 100.0 n  is a 32-bit signed integer, within the range [−2 31 , 2 31  − 1] 需要考量的問題: 1.輸入的指數正負性 2.指數為奇為偶的情況 較直觀且簡易的一般作法 是直接跑Loop 當次方為1時代表保持不變回傳原本輸入的數值 其餘狀況 則判定指數n是否>1 是 ,則依序做n次累乘 否 ,則做累除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package leetcode50_pow ; import java.io.Buffered...