力扣 java题

# 简单 [力扣](https://leetcode-cn.com/problems/reverse-integer/) ## 两数之和 ![image.png](https://cos.easydoc.net/48081513/files/kg5yqycz.png) ``` public static int[] wtl(){ int[] nums ={2, 7, 11, 15}; int target = 9; Map<Integer,Integer> map=new HashMap<Integer,Integer>(); for (int i=0 ;i<nums.length;++i ){ if(map.containsKey(target-nums[i])){ return new int[]{map.get(target-nums[i]),i}; } map.put(nums[i],i); } return null; } ``` > map.containsKey 比较map里的key是否存在; ## 整数反转 ![image.png](https://cos.easydoc.net/48081513/files/kg68zeml.png) ![image.png](https://cos.easydoc.net/48081513/files/kg68zney.png) ``` public static int reverse(int x) { int wtl=0; while (x!=0){ if(wtl*10/10!=wtl){ wtl=0; break; } wtl=wtl*10+x%10; x=x/10; } return wtl; } ``` ## 回文数 ![image.png](https://cos.easydoc.net/48081513/files/kg7blpwt.png) ``` public static boolean reverse(int x) { if (x < 0) { return false; } int w = 0; int t=x; while (x != 0) { w= w * 10 + x % 10; x=x/10; } return w == t; } ``` ## 罗马数字转整数 ![image.png](https://cos.easydoc.net/48081513/files/kg8rdhbs.png) ``` public static int reverse2(String s){ int sum =0; //总值 int presentS=getValue(s.charAt(0)); //当前值 int presentNextS=0; //下一个值 for (int i = 1; i <s.length() ; i++) { presentNextS=getValue(s.charAt(i));//获取下一个值 if (presentS<presentNextS){ //当前值<下一个值 sum-=presentS; }else { sum+=presentS; } presentS=presentNextS; //赋值(当前值=下一个值) } sum+=presentS; //加上最后一个值; return sum; } public static int getValue(char ch) { switch(ch) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: return 0; } } ``` ## 最长公共前缀 ![image.png](https://cos.easydoc.net/48081513/files/kga71ah9.png) ![image.png](https://cos.easydoc.net/48081513/files/kga73kns.png) ``` public static String reverse3(String[] strs){ //纵向查找 if (strs == null || strs.length == 0) { return ""; } char c=0; for (int i = 0; i < strs[0].length() ; i++) { c=strs[0].charAt(i); for (int j = 1; j < strs.length ; j++) { if(strs[j].length()==i || c!=strs[j].charAt(i)){ return strs[0].substring(0,i); } } } return strs[0]; } ```