【Lintcode】1006. Subdomain Visit Count
題目地址:
https://www.lintcode.com/problem/subdomain-visit-count/description
給定一個形如正整數 + 空格 + 域名格式的字串組成的陣列,正整數表示該域名被訪問了多少次。要求對每個域名進行計數,包括每個域名的各個子域名。返回相同格式的字串陣列。
可以用雜湊表進行計數。程式碼如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
/**
* @param cpdomains: a list cpdomains of count-paired domains
* @return: a list of count-paired domains
*/
public List<String> subdomainVisits(String[] cpdomains) {
// Write your code here
Map<String, Integer> map = new HashMap<>();
for (String cpdomain : cpdomains) {
String[] sp = cpdomain.split(" ");
int count = Integer.parseInt(sp[0]);
String domain = sp[1];
map.put(domain, map.getOrDefault(domain, 0) + count);
for (int i = 0; i < domain.length(); i++) {
if (domain.charAt(i) == '.') {
String sub = domain.substring(i + 1);
map.put(sub, map.getOrDefault(sub, 0) + count);
}
}
}
List<String> res = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
res.add(String.valueOf(entry.getValue()) + ' ' + entry.getKey());
}
return res;
}
}
時空複雜度 O ( n l ) O(nl) O(nl), n n n為字串個數, l l l為最長字串長度。
相關文章
- [LeetCode] 811. Subdomain Visit CountLeetCodeAI
- Visit ----.phpPHP
- 泛形variant+visit
- MySQL的COUNT語句--count(*)、 count(常量)、 count(列名)MySql
- count(*)、count(1)和count(列名)的區別
- count (*) 和 count (1) 和 count (列名) 區別
- count(*) 和 count(1)和count(列名)區別
- 圖解MySQL:count(*) 、count(1) 、count(主鍵欄位)、count(欄位)哪個效能最好?圖解MySql
- [20180727]再論count(*)和count(1).txt
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- 7.65 COUNT
- 區分DDD中的Domain, Subdomain, Bounded Context, Problem/Solution SpaceAIContext
- MySQL:count(*) count(欄位) 實現上區別MySql
- SQL Server中count(*)和Count(1)的區別SQLServer
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- Count BFS Graph
- count(*) 優化優化
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- [LintCode] Check Full Binary Tree
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- [LintCode] 3Sum Smaller
- 【Lintcode】1615. The Result of Investment
- [LintCode] Binary Tree Level Order
- 【Lintcode】1736. Throw Garbage
- 【Lintcode】1665. Calculate Number
- 【Lintcode】1789. Distinguish UsernameNGUI
- 【Lintcode】1562. Number of RestaurantsREST
- 【Lintcode】576. Split Array
- 【Lintcode】1267. Lexicographical Numbers
- 【Lintcode】141. Sqrt(x)
- 【Lintcode】1415. Residual Product
- 【Lintcode】1230. Assign CookiesCookie
- 【Lintcode】1732. Snakes and Ladders
- 【Lintcode】1218. Number Complement