给定一个包含大写英文字母和数字的句子,找出这个句子所包含的最大的十六进制整数,返回这个整数的值。数据保证该整数在int表示范围内
public int solve (String s) { // write code here int max = 0; int sum = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) > 'F') { if (sum > max) { max = sum; } sum = 0; continue; } int x = (s.charAt(i) >= 'A') ? 10 + s.charAt(i) - 'A' : s.charAt(i) - '0'; sum = sum * 16 + x; } return max; }
public int solve (String s) { List<String> listMatches = new ArrayList<String>(); Matcher m = Pattern.compile("[0-9a-fA-F]+").matcher(s); while (m.find()) listMatches.add(m.group(0)); Integer maxH = -1; for (String ele: listMatches) { if(ele.length() > 0){ Integer t = Integer.parseInt(ele, 16); if(t > maxH) maxH = t; } } return maxH.intValue(); }
import java.util.*; public class Solution { /** * * @param s string字符串 * @return int整型 */ public int solve (String s) { // write code here int begin = 0; int end = 0; int result = 0; for(int i = 0; end < s.length(); i++) { if ( (Character.isDigit(s.charAt(i))) || (s.charAt(i) >= 'A' && s.charAt(i) <= 'F') ) { end++; result = Math.max(Integer.parseInt(s.substring(begin, end),16), result); } else { begin = i+1; end = begin; } } return result; } }