数字与字符间的转换 Integer.parseInt(a);将a转换成int类型
Integer.toString(a);将a转换成String类型
int[] cp = score.clone();将数组score的值赋给cp
299. 猜数字游戏 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public String getHint (String secret, String guess) { int bulls = 0 ; int [] cntS = new int [10 ]; int [] cntG = new int [10 ]; for (int i = 0 ; i < secret.length(); i++){ if (secret.charAt(i) == guess.charAt(i)) ++bulls; else { ++cntS[secret.charAt(i) - '0' ]; ++cntG[guess.charAt(i) - '0' ]; } } int cows = 0 ; for (int i = 0 ; i < 10 ; ++i){ cows += Math.min(cntG[i], cntS[i]); } return Integer.toString(bulls) + "A" + Integer.toString(cows) + "B" ; } }
412. Fizz Buzz - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution { public List<String> fizzBuzz (int n) { List<String> ans = new ArrayList <>(); for (int i = 1 ; i <= n; i++) { String cur = "" ; if (i % 3 == 0 ) cur += "Fizz" ; if (i % 5 == 0 ) cur += "Buzz" ; if (cur.length() == 0 ) cur = i + "" ; ans.add(cur); } return ans; } }
506. 相对名次 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { String[] ss = new String []{"Gold Medal" , "Silver Medal" , "Bronze Medal" }; public String[] findRelativeRanks(int [] score) { int n = score.length; String[] ret = new String [n]; int [] clone = score.clone(); Arrays.sort(clone); Map<Integer, Integer> map = new HashMap <>(); for (int i = n - 1 ; i >= 0 ; i--) map.put(clone[i], n - 1 - i); for (int i = 0 ; i < n; i++){ int rank = map.get(score[i]); ret[i] = rank < 3 ? ss[rank] : String.valueOf(rank + 1 ); } return ret; } }
539. 最小时间差 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int findMinDifference (List<String> timePoints) { int n = timePoints.size() * 2 ; int [] nums = new int [n]; for (int i = 0 , idx = 0 ; i < n / 2 ; i++, idx += 2 ) { String[] ss = timePoints.get(i).split(":" ); int h = Integer.parseInt(ss[0 ]), m = Integer.parseInt(ss[1 ]); nums[idx] = h * 60 + m; nums[idx + 1 ] = nums[idx] + 1440 ; } Arrays.sort(nums); int ans = nums[1 ] - nums[0 ]; for (int i = 0 ; i < n - 1 ; i++) ans = Math.min(ans, nums[i + 1 ] - nums[i]); return ans; } }
substring()当有两个参数的时候左闭右开区间,当只有一个参数的时候从这个参数开始(闭区间)一直到字符结束位置。
1 2 3 4 5 String ss = "abcdefg" ; String e= ss.substring(0 ,3 );String c = ss.substring(5 ); System.out.println("两个参数:" +e); \\两个参数:abc System.out.println("一个参数:" + c); \\一个参数:fg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public int findMinDifference (List<String> timePoints) { int [] arr = new int [timePoints.size()]; for (int i = 0 ; i < arr.length; i++){ arr[i] = Integer.parseInt(timePoints.get(i).substring(0 , 2 ))*60 + Integer.parseInt(timePoints.get(i).substring(3 )); } Arrays.sort(arr); int min = Integer.MAX_VALUE; for (int i = 1 ; i < arr.length; i++) min = Math.min(min, arr[i] - arr[i - 1 ]); return Math.min(min, arr[0 ] + 1440 - arr[arr.length - 1 ]); } }
553. 最优除法 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public String optimalDivision (int [] nums) { int n = nums.length; StringBuilder bs = new StringBuilder (); for (int i = 0 ; i < n; i++){ bs.append(nums[i]); if (i + 1 < n) bs.append("/" ); } if (n > 2 ){ bs.insert(bs.indexOf("/" ) + 1 , "(" ); bs.append(")" ); } return bs.toString(); } }
537. 复数乘法 - 力扣(LeetCode)
.split()方法的解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 String str1 = "Welcome-To-Shanxi-XiAn" ; String[] ss1 = str1.split("-" ); System.out.println(Arrays.toString(ss1)); String[] sss1 = str1.split("-" , 2 ); System.out.println(Arrays.toString(sss1));String str2 = "www.baidu.com" ; String[] ss2 = str2.split("\\." ); System.out.println(Arrays.toString(ss2));String str3 = "acount=? and uu =? or n=?" ; String[] ss3 = str3.split("and|or" ); System.out.println(Arrays.toString(ss3));String str4 = "1+1i" ; String[] ss4 = str4.split("\\+|i" ); System.out.println(Arrays.toString(ss4));
1 2 3 4 5 6 7 8 9 10 11 12 class Solution { public String complexNumberMultiply (String num1, String num2) { String[] ss1 = num1.split("\\+|i" ), ss2 = num2.split("\\+|i" ); int a = parse(ss1[0 ]), b = parse(ss1[1 ]); int c = parse(ss2[0 ]), d = parse(ss2[1 ]); int A = a * c - b * d, B = b * c + a * d; return A + "+" + B + "i" ; } int parse (String s) { return Integer.parseInt(s); } }
443. 压缩字符串 - 力扣(LeetCode)
代码整的没太懂
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 class Solution { public int compress (char [] cs) { int n = cs.length; int i = 0 , j = 0 ; while (i < n) { int idx = i; while (idx < n && cs[idx] == cs[i]) idx++; int cnt = idx - i; cs[j++] = cs[i]; if (cnt > 1 ) { int start = j, end = start; while (cnt != 0 ) { cs[end++] = (char )((cnt % 10 ) + '0' ); cnt /= 10 ; } reverse(cs, start, end - 1 ); j = end; } i = idx; } return j; } void reverse (char [] cs, int start, int end) { while (start < end) { char t = cs[start]; cs[start] = cs[end]; cs[end] = t; start++; end--; } } }
13. 罗马数字转整数 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public int romanToInt (String s) { int [] gra1 = new int [] {1 , 5 , 10 , 50 ,100 ,500 ,1000 }; char [] gra2 = new char []{'I' , 'V' , 'X' , 'L' , 'C' , 'D' , 'M' }; Map<Character, Integer> map = new HashMap <>(); for (int i = 0 ; i < 7 ; i++) map.put(gra2[i], gra1[i]); int n = s.length(); int max = Integer.MIN_VALUE; int ans = 0 ; for (int i = n - 1 ; i > -1 ; i--){ int v = map.get(s.charAt(i)); if (v >= max){ ans += v; max = v; }else { ans -= v; } } return ans; } }
12. 整数转罗马数字 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public String intToRoman (int num) { int [] value = new int [] {1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 ,1000 }; String[] key = new String []{"I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" ,"CD" ,"D" , "CM" ,"M" }; StringBuilder ret = new StringBuilder (); for (int i = value.length - 1 ; i > -1 ; i--){ int val = value[i]; String symbol = key[i]; while (num >= val){ num -= val; ret.append(symbol); } if (num == 0 ) break ; } return ret.toString(); } }
481. 神奇字符串 - 力扣(LeetCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int magicalString (int n) { int [] cnt = new int [n + 2 ]; cnt[0 ] = 1 ; cnt[1 ] = cnt[2 ] = 2 ; int j = 3 , i = 2 , c = 2 ; while (j < n){ c ^= 3 ; cnt[j++] = c; if (cnt[i++] == 2 ) cnt[j++] = c; } int ans = 0 ; for (int b = 0 ; b < n; b++) ans += 2 - cnt[b]; return ans; } }
上面的这种方法太过于难以像到了,尤其是 C ^= 3 这一句。使用下面进行模拟就比较容易理解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int magicalString (int n) { StringBuilder bs = new StringBuilder ("122" ); String BLX = "1" ; int x = 2 ; while (x < n){ if (bs.charAt(x) == '1' ) bs.append(BLX.repeat(1 )); else bs.append(BLX.repeat(2 )); BLX = BLX == "1" ? "2" : "1" ; x++; } System.out.println(bs.toString()); int ans = 0 ; for (int i = 0 ; i < n; i++) ans += '2' - bs.charAt(i) ; return ans; } }
上面的这种方法对于方法的使用和利用很好