Most Common Word
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example: Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." banned = ["hit"] Output: "ball" Explanation: "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.
Note:
1 <= paragraph.length <= 1000
.1 <= banned.length <= 100
.1 <= banned[i].length <= 10
.- The answer is unique, and written in lowercase (even if its occurrences in
paragraph
may have uppercase symbols, and even if it is a proper noun.) paragraph
only consists of letters, spaces, or the punctuation symbols!?',;.
- Different words in
paragraph
are always separated by a space. - There are no hyphens or hyphenated words.
- Words only consist of letters, never apostrophes or other punctuation symbols.
查看除banned里字符串外频率最多的字符串。
1 class Solution { 2 public: 3 string mostCommonWord(string paragraph, vector<string>& banned) { 4 for(int i = 0; i < paragraph.length(); i ++) { 5 if(!isalpha(paragraph[i])) paragraph[i] = ' '; 6 if(paragraph[i] >= 'A' && paragraph[i] <= 'Z') paragraph[i] += 32; 7 } 8 map<string,int> mp; 9 string s, ss; 10 istringstream sss(paragraph); 11 while(sss >> s) { 12 // cout << s << endl; 13 bool flag = false; 14 for(int i = 0; i < banned.size(); i ++) { 15 if(s == banned[i]) flag = true; 16 } 17 if(!flag) { 18 mp[s]++; 19 } 20 } 21 map<string,int>::iterator it = mp.begin(); 22 int MAX = 0; 23 for(; it != mp.end(); it++) { 24 if((*it).second > MAX) { 25 MAX = (*it).second; 26 ss = (*it).first; 27 } 28 } 29 for(int i = 0; i < ss.length(); i ++) { 30 if(ss[i] >= 'A' && ss[i] <= 'Z') ss[i] += 32; 31 } 32 return ss; 33 } 34 };
Linked List Components
We are given head
, the head node of a linked list containing unique integer values.
We are also given the list G
, a subset of the values in the linked list.
Return the number of connected components in G
, where two values are connected if they appear consecutively in the linked list.
Example 1:
Input: head: 0->1->2->3 G = [0, 1, 3] Output: 2 Explanation: 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
Example 2:
Input: head: 0->1->2->3->4 G = [0, 3, 1, 4] Output: 2 Explanation: 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
Note:
- If
N
is the length of the linked list given byhead
,1 <= N <= 10000
. - The value of each node in the linked list will be in the range
[0, N - 1]
. 1 <= G.length <= 10000
.G
is a subset of all values in the linked list.
求G被分成一个部分。
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 int numComponents(ListNode* head, vector<int>& G) { 12 bool vis[10010]; 13 memset(vis,0,sizeof(vis)); 14 for(int i = 0; i < G.size(); i ++) { 15 vis[G[i]] = true; 16 } 17 int ans = 0, pre, flag = 0; 18 ListNode *p; 19 p = head; 20 while(p) { 21 if(!flag &&vis[p->val]) { 22 ans++; 23 } 24 if(!vis[p->val]) flag = false; 25 else flag = true; 26 p = p->next; 27 } 28 return ans; 29 } 30 };
Ambiguous Coordinates
We had some 2-dimensional coordinates, like "(1, 3)"
or "(2, 0.5)"
. Then, we removed all commas, decimal points, and spaces, and ended up with the string S
. Return a list of strings representing all possibilities for what our original coordinates could have been.
Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with less digits. Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like ".1".
The final answer list can be returned in any order. Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.)
Example 1: Input: "(123)" Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"]
Example 2: Input: "(00011)" Output: ["(0.001, 1)", "(0, 0.011)"] Explanation: 0.0, 00, 0001 or 00.01 are not allowed.
Example 3: Input: "(0123)" Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"]
Example 4: Input: "(100)" Output: [(10, 0)] Explanation: 1.0 is not allowed.
Note:
4 <= S.length <= 12
.S[0]
= "(",S[S.length - 1]
= ")", and the other elements inS
are digits.
模拟题,小数点前不能出现超过两个零,小数点后最后不能出现0
1 class Solution { 2 public: 3 vector<string> ambiguousCoordinates(string S) { 4 vector<string> vs, vs1, vs2; 5 for(int i = 1; i < S.length()-2; i ++) { 6 string s,ss; 7 s = S.substr(1,i); 8 ss = S.substr(i+1,S.length()-i-2); 9 if(s.length() == 1) vs1.push_back(s); 10 else{ 11 for(int j = 1; j <= s.length(); j ++) { 12 string s1, s2; 13 s1 = s.substr(0,j); 14 s2 = s.substr(j,s.length()); 15 if(s1.length()>=2&&s1[0]=='0')continue; 16 if(s2.length()>=2&&s2[s2.length()-1]=='0')continue; 17 if(j == s.length()) { 18 vs1.push_back(s1); 19 continue; 20 } 21 int ans = 0; 22 for(int k = 0; k < s2.length(); k ++) { 23 if(s2[k] == '0') ans++; 24 } 25 if(ans == s2.length()) continue; 26 vs1.push_back(s1+"."+s2); 27 } 28 } 29 if(ss.length() == 1) vs2.push_back(ss); 30 else{ 31 for(int j = 1; j <= ss.length(); j ++) { 32 string ss1, ss2; 33 ss1 = ss.substr(0,j); 34 ss2 = ss.substr(j,ss.length()); 35 if(ss1.length()>=2&&ss1[0]=='0')continue; 36 if(ss2.length()>=2&&ss2[ss2.length()-1]=='0')continue; 37 if(j == ss.length()) { 38 vs2.push_back(ss1); 39 continue; 40 } 41 int ans = 0; 42 for(int k = 0; k < ss2.length(); k ++) { 43 if(ss2[k] == '0') ans++; 44 } 45 if(ans == ss2.length()) continue; 46 vs2.push_back(ss1+"."+ss2); 47 } 48 } 49 //cout << s << ' ' << ss << endl; 50 } 51 for(int i = 0; i < vs1.size(); i ++) { 52 for(int j = 0; j < vs2.size(); j ++) { 53 int ans1 = 0; 54 if(vs1[i].find(".") != string::npos) ans1 = 1; 55 if(vs2[j].find(".") != string::npos) ans1++; 56 if(vs1[i].length()+vs2[j].length() == S.length()-2+ans1) { 57 vs.push_back("("+vs1[i]+", "+vs2[j]+")"); 58 //cout << "("+vs1[i]+", "+vs2[j]+")" << endl; 59 } 60 } 61 } 62 return vs; 63 } 64 };