给定一个包含大写英文字母和数字的句子,找出这个句子所包含的最大的十六进制整数,返回这个整数的值。数据保证该整数在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(); }
class Solution: def solve(self , s ): # write code here import re array = [] for num in re.split(r"[G-Z]", s): num = num.strip().lstrip('0') if num: array.append(int(num.strip().lstrip('0'), 16)) return max(array)
class Solution { public: /** * * @param s string字符串 * @return int整型 */ int solve(string s) { // write code here int res = 0, max = 0; for (int i = 0; i < s.size(); i++) { if (s[i] >= '0' && s[i] <= '9') res = res * 16 + (s[i] - '0'); else if (s[i] >= 'A' && s[i] <= 'F') res = res * 16 + (s[i] - 'A' + 10); else res = 0; if (res > max) max = res; } return max; } };
class Solution { public: /** * * @param s string字符串 * @return int整型 */ int F(string t){ int s=0; for(int i=0;i<t.length();i++){ s *= 16; if(isdigit(t[i])) s += t[i]-'0'; else s += t[i]-'A'+10; } return s; } int solve(string s) { int Max=0; string t=""; for(int i=0;i<s.length();i++){ if((s[i]>='0' && s[i]<='9') || (s[i]>='A' && s[i]<='F')){ t += s[i]; }else{ Max = max(Max, F(t)); t = ""; } } if(t!="") Max = max(Max, F(t)); return Max; } };
# # # @param s string字符串 # @return int整型 # import re class Solution: def solve(self, s): regex=re.compile(r"[0-9a-fA-F]+") ans=regex.findall(s) m=-1 for i in ans: if(int(i,16)>m): m=int(i,16) return m
# @param s string字符串 # @return int整型 # class Solution: def solve(self , s ): # write code here nums = [] i=0 for j in range(0,len(s)): if '0'<=s[j]<='9' or 'A'<=s[j]<='F': continue if i>j:continue nums.append(s[i:j]) i=j+1 while i<len(s): if not ('0'<=s[i]<='9' or 'A'<=s[i]<='F'): i+=1 continue break res = 0 for x in nums: if len(x)==0:continue res = max(res,int(x,16)) return res
function hex2int(hex){ var len=hex.length,a=new Array(len),code; for (var i=0;i<len;i++){ code = hex.charCodeAt(i); if (48<=code&&code<58){code-=48;} else{code=(code&0xdf)-65+10;} a[i]=code; } return a.reduce(function(acc,c) {acc=16*acc+c;return acc;},0); } function solve(s) { let array = s.match(/[1-9A-F][\dA-F]*/g); if (array==null){return 0;} array.sort((a,b)=>{return a.length-b.length}).reverse(); let strLen = array[0].length; let arrLen = array.length; let hexArray = []; for(let i=0;i<arrLen;i++) { if(array[i].length<strLen)break; hexArray.push(hex2int(array[i])); } hexArray.sort().reverse(); return hexArray[0]; }
import sys sys.setrecursionlimit(3000) flag_num = 0 str_list = [chr(x) for x in range(65, 71)] + [str(y) for y in range(0, 10)] target_list = [] class Solution: def solve(self, s): global flag_num pointer1 = pointer2 = flag_num while pointer1 < len(s) and s[pointer1] == '0': flag_num += 1 pointer1 = pointer2 = flag_num while pointer2 < len(s) - 1 and s[pointer2] in str_list: pointer2 += 1 if pointer1 != pointer2: target_list.append(s[pointer1:pointer2]) if flag_num < len(s): flag_num = pointer2 + 1 self.solve(s) res = max(target_list, key=lambda item: int(item, 16)) return int(res, 16)
func solve(s string) int { var res uint64 = 0 max := 0 for i := 0; i < len(s); i++ { if illegal(s[i]){ continue } for j:=i+1;j<len(s);j++{ if illegal(s[j]){ tmp:=s[i:j] res ,_=strconv.ParseUint(tmp,16,32) if int(res)>max{ max=int(res) } } } } return max } func illegal(i byte) bool { if i == 48 || i == 49 || i == 50 || i == 51 || i == 52 || i == 53 || i == 54 || i == 55 || i == 56 || i == 57 || i == 65 || i == 66 || i == 67 || i == 68 || i == 69 || i == 70 { return false } return true }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ public int solve (String s) { // write code here int max = Integer.MIN_VALUE; StringBuilder sb = new StringBuilder(); for(char c:s.toCharArray()){ if((c>='0' && c<='9') || (c>='A' && c<='F')){ sb.append(c); }else{ if(sb.length()==0)continue; int m = Integer.parseInt(sb.toString(),16); max = Math.max(m,max); sb = new StringBuilder(); } } return max; } }
class Solution: def solve(self , s ): # write code here res = ''.join(list(map(lambda x: x if 48<=ord(x)<=57&nbs***bsp;65<=ord(x)<=70 else "#", s))) res = res.split("#") res = list(map(lambda x:int(x, 16) if x != '' else 0, res)) return max(res)
keymap = [10,11,12,13,14,15] class Solution: def solve(self , s ): # write code here if s is None&nbs***bsp;len(s)==0: return 0 left = 0 cur = 0 res = 0 while left<len(s): if "0"<=s[left]<="9": cur = cur*16+int(s[left]) left+=1 elif "A"<=s[left]<="F": cur = cur*16+keymap[ord(s[left])-ord("A")] left+=1 else: cur = 0 left+=1 res = max(res,cur) return res