Look for special circumstances, Category talk: three cases
1) the difference between the lengths of the two strings is greater than a direct return false;
2) difference in length is equal to 1, the length is determined as a character string is not deleted, if the remaining character strings;
3) The difference in length is equal to 0, it determines the number of different characters, if more than one return false.
class Solution { public: /** * @param s: a string * @param t: a string * @return: true if they are both one edit distance apart or false */ bool isOneEditDistance(string &s, string &t) { // write your code here if(s.size() > t.size()) swap(s, t); //保证t比s长 int diff = t.size() - s.size(); if(diff > 1) return false; if(== the diff . 1 ) { for ( int I = 0 ; I <s.size (); I ++ ) { IF (! T [I] = S [I]) return (s.substr (I) == T. substr (i + . 1 )); // T remaining after removing the index of the character i and s are the same substring } return to true ; } iF (the diff == 0 ) { int CNT = 0 ; for ( int i = 0 ; I <s.size (); I ++ ) { IF(s[i] != t[i]) cnt ++; } return (cnt==1); } } };
Meaning of the questions: API: int read4 (char * buf) each read four characters, each time you want to realize read function can be read into any character.
ptr is a pointer to the read character string, bufferPtr points to buffer queue head pointer, bufferCnt points to buffer queue tail pointer.
// Forward declaration of the read4 API. int read4(char *buf); //return the actual number of characters read class Solution { public: /** * @param buf destination buffer * @param n maximum number of characters to read * @return the number of characters read */ char buffer[4]; int bufferPtr = 0, bufferCnt = 0; //队列的头指针和尾指针 int read(char *buf, int n) { // the Write your code here Wallpaper int PTR = 0 ; the while (PTR < n-) { IF (bufferPtr == bufferCnt) { // queue is empty, into the team bufferCnt = read4 (Buffer); bufferPtr = 0 ; } IF (= bufferCnt = 0 ) // no data reading exit BREAK ; the while (PTR <&& n-bufferPtr < bufferCnt) { buf [PTR] = Buffer [bufferPtr]; PTR ++; bufferPtr++; } } return ptr; } };
class Solution { public: /* * @param strs: a list of strings * @return: encodes a list of strings to a single string. */ string encode(vector<string> &strs) { // write your code here string ans; for(int i=0; i<strs.size(); i++){ string s = strs[i]; for(int j = 0; j<s.size(); j++){ if(s[j] == ':' ) ANS + = " :: " ; // add an escape character, all the: becomes :: the else ANS + = S [J]; } ANS + = " :; " ; // one end of the string after adding delimiter } return ANS; } / * * @param STR: A String * @return: A SINGLE dcodes String List of strings to A * / Vector < String > decode ( String & STR) { // write your code here vector<string> ans; string item; int i = 0; while(i<str.size()){ if(str[i] == ':'){ if(str[i+1] == ';'){ // ; 为分隔符 ans.push_back(item); item = ""; i += 2; } the else { // : an escape character Item + STR = [I + . 1 ]; I + = 2 ; } } the else { Item + = STR [I]; I ++ ; } } return ANS; } };
leetcode 388 & lintcode 643
class Solution { public : Vector < String > Split ( String & STR, char C) { char * CSTR, * P; Vector < String > RET; CSTR = new new char [str.size () + . 1 ]; strcpy (CSTR, str.c_str ()); // the copy to str cstr P = strtok (cstr, & c); // to point to the character array in cstr c divides the while (P =! NULL) { ret.push_back (P); P = strtok (NULL, &C); } return RET; } int lengthLongestPath ( String INPUT) { int ANS = 0 ; Vector < String > Split Lines = (INPUT, ' \ n ' ); // with \ n string to separate Map < int , int > level_size; level_size [ - . 1 ] = 0 ; // initializes the layer 0 for ( int I = 0 ; I <lines.size (); I ++ ) { String Line = Lines [I]; int Level = line.find_last_of ( ' \ T ' ) + . 1 ; // Find the last to appear in the string \ t. There is a match, the matching position; otherwise, returns -1. Int len = (line.substr (level)) size ();. // the length of the string level layer IF (line.find ( ' . ' !) = String :: NPoS) {// IF (s.find ( '.') = -1)! // find the file is described. ANS = max (ANS, level_size [Level - . 1 ] + len); } the else { / / each line to be added / so +1 level_size [Level] = level_size [Level - . 1 ] + len + . 1 ; // prefix and optimization } } return ANS; } };
Meaning of the questions: There are n people on the party, numbered from 0 to n. There is a celebrity, and the remaining n-1 know him personally, but he did not know that n-1 individual. Now you want to use the least number of times to find who is the celebrity.
It requires written in O (n) time.
Ideas: There are a lot of redundancy.
If celebrities do test for 1: 1, 3, 4 understanding; 1 and 5 do not know.
Description 1 is not a celebrity, and 2,3,4 will not be a celebrity.
I.e. two people, one person is always removed: if 1 and 2, 2 1 Note 2 is not recognized celebrity; do not know if and 3,3 1 1 1 are not described celebrity.
// Forward declaration of the knows API. bool knows(int a, int b); //return whether A knows B //if true, A is not celebrity //if false, B is not celebrity class Solution { public: /** * @param n a party with n people * @return the celebrity's label or -1 */ int findCelebrity(int n) { // Write your code here int ans = 0; for(int i=1; I <n-; i ++ ) { IF (Knows (ANS, i)) // 0 personal knowledge of person i 0 individuals not described celebri ANS = i; // every time a person removing // finally obtained a person } // do on the last remaining top celebrities who once tested for ( int i = 0 ; i <the n-; i ++ ) { IF (ANS = i &&! Knows (ANS, i)) return - 1 ; IF ! (ANS = && I! Knows (I, ANS)) return - . 1 ; } return ANS; } };
class Solution { public: int romanToInt(string s) { unordered_map<char, int> mp = {{'I',1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}}; int r = mp[s[0]]; for(int i=1; i<s.size(); i++){ r += mp[s[i]]; if(mp[s[i-1]] < mp[s[i]]) r = r - 2*mp[s[i-1]]; } return r; } };
class Solution { public: int romanToInt(string s) { unordered_map<string, int> mp = {{"I",1}, {"V", 5}, {"X", 10}, {"L", 50}, {"C", 100}, {"D", 500}, {"M", 1000}}; int r = mp[s.substr(0, 1)]; for(int i=1; i<s.size(); i++){ r += mp[s.substr(i, 1)]; if(mp[s.substr(i-1, 1)] < mp[s.substr(i, 1)]) r -= 2*mp[s.substr(i-1, 1)]; } return r; } };
class Solution { public: string intToRoman(int num) { vector<int> value = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5,4,1}; vector<string> roman = {"M", "CM", "D","CD","C","XC","L","XL","X","IX","V","IV", "I"}; string str; int i = 0; while(i<value.size()){ if(num >= value[i]){ num -= value[i]; str += roman[i]; } else i++; } return str; } };