packagetop.bitqian.easy.palindrome;importjava.util.Stack;/*** @author echo lovely* @date 2021/11/8 14:52* @description 回文數* https://leetcode-cn.com/problems/palindrome-number/*/publicclassPalindrome{publicstaticvoidmain(String[] args){boolean b =isPalindrome(19091);System.out.println(b);System.out.println(upgrade(0));}// 使用棧解題publicstaticbooleanisPalindrome(int x){if(x <0){returnfalse;}String str =String.valueOf(x);Stack<String> stack =newStack<>();for(String s : str.split("")){stack.push(s);}String newStr ="";for(int i = stack.size()-1; i >=0; i--){newStr = newStr.concat(stack.pop());}return newStr.equals(str);}/*** https://leetcode-cn.com/problems/palindrome-number/solution/hui-wen-shu-by-leetcode-solution/*/publicstaticbooleanupgrade(int x){// 特殊情況:// 如上所述,當 x < 0 時,x 不是回文數。// 同樣地,如果數字的最后一位是 0,為了使該數字為回文,// 則其第一位數字也應該是 0// 只有 0 滿足這一屬性if(x <0||(x %10==0&& x !=0)){returnfalse;}int revertedNumber =0;while(x > revertedNumber){revertedNumber = revertedNumber *10+ x %10;x /=10;}// 當數字長度為奇數時,我們可以通過 revertedNumber/10 去除處于中位的數字。// 例如,當輸入為 12321 時,在 while 循環的末尾我們可以得到 x = 12,revertedNumber = 123,// 由于處于中位的數字不影響回文(它總是與自己相等),所以我們可以簡單地將其去除。return x == revertedNumber || x == revertedNumber /10;}}
3. 將整數倒轉
注意整數有最大值和最小值!!!, 越界報錯,用Long比較。 這里也是利用棧解決。
packagetop.bitqian.easy.reverse_integer;importjava.util.Stack;/*** @author echo lovely* @date 2021/11/8 11:36* @description 兩數反轉* https://leetcode-cn.com/problems/reverse-integer/* 1. Integer.MIN_VALUE的絕對值不靠譜* 2. Integer.parseInt("9646324351") 轉換過大數字, 可能出現異常*/publicclassReverseInteger{publicstaticvoidmain(String[] args){// the min value of integer, if you get a abs, it's will be a negative num..int val =-2147483648;int res =reverse(1534236469);System.out.println(res);// 結果是負數..System.out.println(-val);System.out.println(Math.abs((long) val));res =upgrade(val);System.out.println(res);}// 使用棧進行反轉publicstaticintreverse(int x){if(x ==0)return x;if(x <=Integer.MIN_VALUE || x >=Integer.MAX_VALUE){return0;}String s =String.valueOf(Math.abs(x));Stack<String> stack =newStack<>();for(String ele : s.split("")){stack.push(ele);}s ="";for(int i = stack.size()-1; i >=0; i--){s = s.concat(stack.pop());}// 關于證數轉換問題, 如果數字超出整數范圍, 會報轉換錯誤異常。// Integer.valueOf("9646324351");// Exception in thread "main" java.lang.NumberFormatException: For input string: "9646324351"long tmp =Long.parseLong(s);boolean flag = tmp >Integer.MAX_VALUE || tmp <Integer.MIN_VALUE;if(flag){return0;}if(x >0){x =Integer.parseInt(s);return x;}x =Integer.parseInt(s);return-x;}/*** https://leetcode-cn.com/problems/reverse-integer/solution/zheng-shu-fan-zhuan-by-leetcode-solution-bccn/*/publicstaticintupgrade(int x){int rev =0;while(x !=0){if(rev <Integer.MIN_VALUE /10|| rev >Integer.MAX_VALUE /10){return0;}int digit = x %10;x /=10;rev = rev *10+ digit;}return rev;}}
4. 羅馬轉整數
利用字典map存儲,羅馬和數字一一對應。
求出特殊的羅馬數字組合,比如IV, 要用減法, IV是4
求加法的羅馬數字,VI,就是6
packagetop.bitqian.easy.roman_to_integer;importjava.util.HashMap;importjava.util.Map;/*** @author echo lovely* @date 2021/11/8 15:07* @description https://leetcode-cn.com/problems/roman-to-integer/*/publicclassRomanToInteger{publicstaticvoidmain(String[] args){System.out.println(romanToInt("IV"));System.out.println(upgrade("XIV"));/*I 1V 5X 10L 50C 100D 500M 1000rules:I 可以放在 V (5) 和 X (10) 的左邊,來表示 4 和 9。X 可以放在 L (50) 和 C (100) 的左邊,來表示 40 和 90。 C 可以放在 D (500) 和 M (1000) 的左邊,來表示 400 和 900。*/}staticMap<String,Integer> paramMap =newHashMap<>();static{paramMap.put("I",1);paramMap.put("V",5);paramMap.put("X",10);paramMap.put("L",50);paramMap.put("C",100);paramMap.put("D",500);paramMap.put("M",1000);}publicstaticintromanToInt(String s){int res =0;String[] specArr =newString[]{"IV","IX","XL","XC","CD","CM"};// 1. 計算指定的字符for(String ele : specArr){if(s.contains(ele)){res +=getVal(ele);s = s.replace(ele,"");}}// 2. 計算正常相加的字符for(String ele : s.split("")){// IV, 防止s是""if("".equals(ele))return res;Integer val = paramMap.get(ele);res += val;}return res;}privatestaticintgetVal(String ele){int val =0;String[] node = ele.split("");for(String tmp : node){Integer i = paramMap.getOrDefault(tmp,0);val =Math.abs(val - i);}return val;}/*https://leetcode-cn.com/problems/roman-to-integer/solution/luo-ma-shu-zi-zhuan-zheng-shu-by-leetcod-w55p/*/publicstaticintupgrade(String s){int ans =0;int n = s.length();for(int i =0; i < n;++i){int value = symbolValues.get(s.charAt(i));if(i < n -1&& value < symbolValues.get(s.charAt(i +1))){ans -= value;}else{ans += value;}}return ans;}staticMap<Character,Integer> symbolValues =newHashMap<Character,Integer>(){privatestaticfinallong serialVersionUID =4834716094584504863L;{put('I',1);put('V',5);put('X',10);put('L',50);put('C',100);put('D',500);put('M',1000);}};}