Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value. Return the minimum number of operations to make all elements of nums equal to 1. If it is impossible, return -1. The gcd of two integers is the greatest common divi...
原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with ...
题目地址:https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/ 题目描述 In a row of dominoes,A[i]andB[i]represent the top and bottom halves of thei-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the...
1classSolution {2func findRestaurant(_ list1: [String], _ list2: [String]) ->[String] {3vardict =[String: Int].init(minimumCapacity: list1.count)4vardict1 =[String: Int].init(minimumCapacity: max(list1.count, list2.count))567for(index,s)inlist1.enumerated() {8dict[s] =index9}...
124. Binary Tree Maximum Path Sum; 543. Diameter of Binary Tree; 687. Longest Univalue Path;求二叉树的最大路径,路径和,相同值节点的最大路径。 解法:Binary Tree (二叉树) 模版:一棵树问题: 1def solve(root)2//无效节点处理3ifnot root:return...4//递归终点,base5iff(root):return...6//左...
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1). 参考答案 1classSolution {2public:3vector<string> findRestaurant(vector<string>& list1, vector<string>&list2) {45vector<string>res;6unordered_map<string,int>map;7for(size_t i ...
Solution one: hash table + heap This is the second problem of leetcode weekly contest 34. We need data structures to solve it. There is two work for us to do: Find the common interests ---> hash table(unordered_map) Find the minimal index sum among all common interests ---> heap(...
1classSolution {2func mask(_ a : Int, _ b : Int ) ->Int {3return(1<< a) | (1<<b)4}5func minDominoRotations(_ A: [Int], _ B: [Int]) ->Int {6guard A.count >0&& A.count == B.countelse{7return-18}9vartotalMask =2551011for(a,b)inzip(A, B) {12totalMask = tot...