摘要:暴力解法就是時(shí)靈時(shí)不靈,兩次一次。希望看到的大神能夠分享優(yōu)質(zhì)的解法謝謝大家
Problem
For an array A, if i < j, and A[i] > A[j], called (A[i], A[j]) is a reverse pair.
return total of reverse pairs in A.
Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3
Note暴力解法就是時(shí)靈時(shí)不靈,submit兩次ac一次。
希望看到的大神能夠分享優(yōu)質(zhì)的解法^_^ linspiration1991@gmail.com
public class Solution { public long reversePairs(int[] A) { long res = 0; int n = A.length; for (int i = 0; i < n-1; i++) { for (int j = i; j < n; j++) { if (A[i] > A[j]) res += 1; } } return res; } } //謝謝大家!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65842.html
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
摘要:兩數(shù)之和問(wèn)題各變種多解法小結(jié)聲明文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處兩數(shù)之和等于題目大意給出未排序數(shù)組和指定目標(biāo),返回?cái)?shù)組中兩數(shù)之和的組合元素下標(biāo)要求下標(biāo)從開始,而且,保證題目中有且只有個(gè)可行解解法暴力時(shí)間復(fù)雜度求解解題思路暴力二重循環(huán)求解 兩數(shù)之和問(wèn)題各變種多解法小結(jié) 聲明 文章均為本人技術(shù)筆記,轉(zhuǎn)載請(qǐng)注明出處:[1] https://segmentfault.com/u/yzwal...
Problem Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both nu...
Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...
摘要:指針為,我們選擇的兩個(gè)結(jié)點(diǎn)是和。要注意循環(huán)的邊界條件,這兩個(gè)結(jié)點(diǎn)不能為空。主要思路是先用和兩個(gè)新結(jié)點(diǎn)去保存和兩個(gè)結(jié)點(diǎn)。完成交換之后,連接和,并讓前進(jìn)至此時(shí)的結(jié)點(diǎn)。 Problem Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you sh...
閱讀 1408·2021-09-23 11:21
閱讀 3105·2019-08-30 14:14
閱讀 3187·2019-08-30 13:56
閱讀 4136·2019-08-30 11:20
閱讀 1949·2019-08-29 17:23
閱讀 2765·2019-08-29 16:14
閱讀 1693·2019-08-28 18:18
閱讀 1490·2019-08-26 12:14