LintCode: Longest Words

C++

 1 class Solution {
 2 public:
 3     /**
 4      * @param dictionary: a vector of strings
 5      * @return: a vector of strings
 6      */
 7     vector<string> longestWords(vector<string> &dictionary) {
 8         // write your code here
 9         if (dictionary.size() <= 1) {
10             return dictionary;
11         }
12
13         vector<string> result;
14         int max_len = 0, cur_len;
15
16         for (int i=0; i<dictionary.size(); i++) {
17             cur_len = dictionary[i].size();
18             if(cur_len < max_len) {
19                 continue;
20             }
21             if(cur_len > max_len) {
22                 result.clear();
23                 max_len = cur_len;
24             }
25             result.push_back(dictionary[i]);
26         }
27         return result;
28     }
29 };
时间: 2024-08-25 14:58:25

LintCode: Longest Words的相关文章

Lintcode:Longest Common Subsequence 解题报告

Longest Common Subsequence Given two strings, find the longest comment subsequence (LCS). Your code should return the length of LCS. 样例For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1 For "ABCD" and "

[LintCode] Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Example Fo

[LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Notice O(n

LintCode Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is to find a

[LintCode] Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Have you met this question in a real interview? Yes Example For example, the longest substring without repeating letters for"abcabcbb" is "abc", whi

LintCode &quot;Longest Increasing Continuous subsequence II&quot; !!

DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vector<int>>& A, vector<vector<int>>& dp) { if(dp[i][j] != 0) return dp[i][j]; if (i > 0 && A[i-1][j] > A[i][j]) { dp

[Coding Made Simple] Longest Increasing Subsequence

Given an array, find the longest increasing subsequence in this array. The same problem link. [LintCode] Longest Increasing Subsequence

lintcode 中等题:longest common substring 最长公共子串

题目 最长公共子串 给出两个字符串,找到最长公共子串,并返回其长度. 样例 给出A=“ABCD”,B=“CBCE”,返回 2 注意 子串的字符应该连续的出现在原字符串中,这与子序列有所不同. 解题 注意: 子序列:这个序列不是在原字符串中连续的位置,而是有间隔的,如:ABCDE  和AMBMCMDMEM 最长公共子序列是ADCDE 子串:子串一定在原来字符串中连续存在的.如:ABCDEF 和SSSABCDOOOO最长公共子串是ABCD 参考链接,讲解很详细 根据子串定义,暴力破解 public

[LintCode] System Longest File Path

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory