leetcode [#396] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (10)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of integers A and let n to be its length.
Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow:
F(k) = 0 * Bk[0] + 1 * Bk[1] + … + (n-1) * Bk[n-1].
Calculate the maximum value of F(0), F(1), …, F(n-1).
Example:A = [4, 3, 2, 6]
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16F(2) = (0 * 2) + (1...阅读全文

leetcode [#414] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (12)
目录1. 题目2. 解决方案3. 注意事项
题目Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example1:Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.
Example2:Input: [1, 2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example3:Input: [2, 2, 3, 1]
Output: 1
Explanation: Note that the third maximum here means the third ma...阅读全文

leetcode [#409] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (15)
目录1. 题目2. 解决方案3. 注意事项
题目Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not considered a palindrome here.
Example1:Input:“abccccdd”
Output:7
Explanation:One longest palindrome that can be built is “dccaccd”, whose length is 7.
解决方案123456789101112131415161718192021222324252627282930public class Solution {    public int longestPalindrome(String s) {     ...阅读全文

leetcode [#438] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (15)
目录1. 题目2. 解决方案3. 注意事项
题目Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.The order of output does not matter.
Example1:Input:s: “cbaebabacd” p: “abc”
Output:[0, 6]
Explanation:The substring with start index = 0 is “cba”, which is an anagram of “abc”.The substring with start index = 6 is “bac”, which is an anagram of “abc”.
Example...阅读全文

leetcode [#415] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (19)
目录1. 题目2. 解决方案3. 注意事项
题目Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Example:Input:[[0,0],[1,0],[2,0]]
Output:2
Explanation:The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0]...阅读全文