def second_largest_number(s): first = -1 second = -1for char in s: if char.isdigit(): num = int(char) if num > first: second = first first = num elif num > second and num < first: second = numreturn second if second != -1 else -1解释: 首先,我们将first和second初始化为-...
string res ="";for(PR pr : lst){ string s = pr.first;intcnt = pr.second;for(inti =0; i < cnt; i++){ res += s; } }if(res[0] =='0'){return"0"; }else{returnres; } } };
比如[2,3,4],3乘以2等于6大于4,不符合题目要求,就不需要比较2了。 publicintdominantIndex(int[] nums) {intmax =0, second =0,index=0;for(inti=0; i<nums.length; i++) {if(nums[i] > max) { second = max; max = nums[i];index= i; }elseif(nums[i] > second) { second = nums...
In a given integer array nums, there is always exactly one largest element. Find whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, otherwise return -1. Example 1: Input: nums = [...
class Solution { public: int secondHighest(string s) { set<char> cnt; for(auto c : s){ if(c >= '0' && c <= '9'){ cnt.insert(c); } } if(cnt.size() <= 1) return -1; auto it = cnt.end(); it--; it--; return *it - '0'; } }; 5694. 设计一个验证系统 题目...
n-2), subRob(nums, 1, n-1)); } public int subRob(int[] nums, int s, int e){ int first=nums[s], second=Math.max(first, nums[s+1]); for (int i=s+2; i<=e; i++){ int tem = Math.max(nums[i]+first, second); first = second; second = tem; } return second; }...
struct Node { int first, second; Node(){} Node(int f, int s) { first = f; second = s; } bool operator < (const Node tmp) const { if (first != tmp.first) { return first > tmp.first; } else { return second > tmp.second; } } }; class MedianFinder { public: priority_qu...
You may assume the number of calls to update and sumRange function is distributed evenly. 【解答】写的代码看起来有点啰嗦,大致思路是线段树。也就是说,每个节点都存放一个 sum,这样在求一段区间的和的时候会比较快;而在更新某一个数的时候,也只需要更新整个树从上到下的一条路径即可。 代码语...
count of smaller number after self: merge sort can count the numbers to the right/left when merging misc largest number: Just sort. Use a_b and b_a to decide which comes at first wiggle sort: sort pair by pair wiggle sort II: Quick select algorithm to find the kth largest element in...
(int second = first + 1; second < n; ++second) { // 需要和上一次枚举的数不相同 if (second > first + 1 && nums[second] == nums[second - 1]) { continue; } // 需要保证 b 的指针在 c 的指针的左侧 while (second < third && nums[second] + nums[third] > target) { --third...