「Leetcode」976. Largest Perimeter Triangle(C++)
分析
好久不刷題真的思維僵化,要考慮到這樣一個結論:如果遞增的三個數\(x_i,x_{i+1},x_{i+2}\)不符合題意,那麽最大的兩邊之差一定大於等於第一條邊,那麽任何比第一條邊小的都不能成立。這樣一來,遞增排序,然後線性找就可以了。
代碼
class Solution { public: int largestPerimeter(vector<int>& A) { int ans=0; sort(A.begin(),A.end()); for(int i=A.size()-3;i>=0;i--) { if(A[i]+A[i+1]>A[i+2]) { ans = A[i]+A[i+1]+A[i+2]; break; } } return ans; } };
「Leetcode」976. Largest Perimeter Triangle(C++)
相關推薦
「Leetcode」976. Largest Perimeter Triangle(C++)
mat break triangle size 大於 不能 clas angle largest 分析 好久不刷題真的思維僵化,要考慮到這樣一個結論:如果遞增的三個數\(x_i,x_{i+1},x_{i+2}\)不符合題意,那麽最大的兩邊之差一定大於等於第一條邊,那麽任何比
「LeetCode」0003-Add Two Numbers(Typescript)
分析 程式碼 /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers=function(l1, l2) { let dummyHead = new ListNod
「Leetcode」975. Odd Even Jump(Java)
.get result 元素 jump 依然 情況 函數 true test 分析 註意到跳躍的方向是一致的,所以我們需要維護一個數接下來跳到哪裏去的問題。換句話說,就是對於一個數\(A_i\),比它大的最小值\(A_j\)是誰?或者反過來。 這裏有兩種方案,一種是單調棧,
「Leetcode」13. Roman to Integer(Java)
char () 情況 n-1 urn 一個 default ++i 所表 分析 把具體的情況一個一個實現即可,沒有什麽幺蛾子。 代碼 class Solution { public int romanToInt(String s) { int ans
Leetcode 976. Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengt
119th LeetCode Weekly Contest Largest Perimeter Triangle
Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it
LeetCode | Reverse Words in a String(C#)
++ style str blog cnblogs count item leetcode string 題目: Given an input string, reverse the string word by word. For example,Given s = "
【LeetCode】40. Combination Sum II(C++)
地址:https://leetcode.com/problems/combination-sum-ii/ 題目: Given a collection of candidate numbers (candidates) and a target number (target), fi
【LeetCode】29. Divide Two Integers(C++)
地址:https://leetcode.com/problems/divide-two-integers/ 題目: Given two integers dividend and divisor, divide two integers without using multiplic
【LeetCode】73. Set Matrix Zeroes(C++)
地址:https://leetcode.com/problems/set-matrix-zeroes/ 題目: Given a m m
【LeetCode】64. Minimum Path Sum(C++)
地址:https://leetcode.com/problems/minimum-path-sum/ 題目: Given a m ∗
【LeetCode】63. Unique Paths II(C++)
地址:https://leetcode.com/problems/unique-paths-ii/ 題目: A robot is located at the top-left corner of a m
【LeetCode】93. Restore IP Addresses(C++)
地址:https://leetcode.com/problems/restore-ip-addresses/ 題目: Given a string containing only digits, restore it by returning all possible valid I
【LeetCode】743. Network Delay Time(C++)
題目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges times[i] = (u, v, w), where
【LeetCode】754. Reach a Number(C++)
題目: You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go
【LeetCode】10. Regular Expression Matching(C++)
地址:https://leetcode.com/problems/regular-expression-matching/ 題目: Given an input string (s) and a pattern (p), implement regular expression ma
【LeetCode】5. Longest Palindromic Substring(C++)
題目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1:
【LeetCode】32. Longest Valid Parentheses(C++)
地址:https://leetcode.com/problems/longest-valid-parentheses/ 題目: Given a string containing just the characters '(' and ')', find the length of
【LeetCode】76. Minimum Window Substring(C++)
地址:https://leetcode.com/problems/minimum-window-substring/ 題目: Given a string S and a string T, find the minimum window in S which will contai
【LeetCode】45. Jump Game II(C++)
地址:https://leetcode.com/problems/jump-game-ii/ 題目: Given an array of non-negative integers, you are initially positioned at the first index of