LeetCode-- algorithm, simple Level 1

#include " the stdafx.h " 
#include " LeetCode_S.h " 
#include <unordered_map> 
#include <math.h> 
#include <Stack> the using namespace STD; 
CLeetCode_Solution * CLeetCode_Solution instance :: = NULL; 
CLeetCode_Solution :: CLeetCode_Solution () 
{ 
    printf ( " the Hello LeetCode \ the n- " ); 
} 
CLeetCode_Solution :: ~ CLeetCode_Solution () 
{ 
} / * 
 given an integer array nums and a target value target, and to ask you to find the target in the array that two integers, and return to their array subscript.



 






You can assume that each input corresponds to only one answer. However, you can not re-use the same array element. 

Example: 

Given nums = [2, 7, 11 , 15], target = 9 

because nums [0] + nums [1 ] = 2 + 7 = 9 
is returned [0,. 1] 
 * / 
STD :: Vector < int > :: CLeetCode_Solution twoSum (Vector < int > the nums &, int target) 
{ 
    unordered_map < int , int > m;
     for ( int I = 0 ; I <nums.size (); I ++ ) 
    { 
        / * 
         * for (int J I +. 1 =; J <nums.size (); J ++) 
        { 
            IF (target == (the nums [I] + the nums [J])) 
            { 
                ret.push_back (I);
                ret.push_back (J); 
            } 
        } * / 
        IF (! m.find (target - the nums [I]) = m.end ()) 
        { 
            return {m [target - the nums [I]], I}; 
        } 
        m [the nums [I]] = I; 
    } 
    return { 0 , 0 }; 
} 

/ * 
 is given a 32-bit signed integer, you need this integer number on each inverted. 

Example 1: 

Input: 123 
Output: 321 
 Example 2: 

Input: -123 
Output: -321 
* / 
int CLeetCode_Solution :: Reverse ( int X) 
{ 
    int RET ( 0);
    while (x != 0)
    {
        int pop = x % 10;
        x /= 10;
        if (ret > INT_MAX / 10 || (ret == INT_MAX / 10 && pop > 7)) return 0;
        if (ret < INT_MIN / 10 || (ret == INT_MIN / 10 && pop - 8)) return 0;
        ret = ret * 10 + pop;
    }
    return ret;
}
/*
 *判断回文数
 **/
bool  CLeetCode_Solution::isPalindrome(int x) {
    if (x < 0)
    {
        return false;
    }
    long long rst(0);
    int x_tmp = x;
    while (x != 0)
    {
        int pop = x % 10;
        rst = rst * 10 + pop;
        x /= 10;
    }
    return (rst == x_tmp);
}
//二进制相加
std::string CLeetCode_Solution::addBinary(string a, string b)
{
    int al = a.size();
    int bl = b.size();
    while (al < bl)
    {
        a = '0' + a;
        ++al;
    }
    while (bl < al)
    {
        b = '0' + b;
        ++bl;
    }
    for (int j = a.size() - 1; j > 0; j--)
    {
        a[j] = a[j] - '0' + b[j];
        if (a[j] >= '2')
        {
            a[j] = (a[j] - '0') % 2 + '0';
            a[j - 1] = a[j - 1] + 1;
        }
    }
    a[0] = a[0 ] - ' 0 ' + B [ 0 ]; // the sum ab bit 0 
    IF (A [ 0 ]> = ' 2 ' ) // if greater than or equal to 2, into a required 
    { 
        A [ 0 ] = (A [ 0 ] - ' 0 ' )% 2 + ' 0 ' ; 
        A = ' . 1 ' + A; 
    } 
    return A; 
} // turn Roman numerals Arabic numerals
 

int CLeetCode_Solution :: romanToInt ( String S) { //顺序计算 /* unordered_map<string, int> m; m = { { "I", 1 },{ "IV", 3 },{ "IX", 8 },{ "V", 5 },{ "X", 10 },{ "XL", 30 },{ "XC", 80 },{ "L", 50 },{ "C", 100 },{ "CD", 300 },{ "CM", 800 },{ "D", 500 },{ "M", 1000 } }; int ret = m[s.substr(0, 1)]; for (int i = 1; i < s.size(); i++) { string two = s.substr(i - 1, 2); string one = s.substr(i, 1); ret += m[two] ? m[two] : m[one]; } return ret; * / explains: L = 50, V =. 5, III = 3. output: 58 * input: "LVIII"/ *reverse calculation (better speed and memory), Wang Qianmian value from behind, each taking one symbol, if the ratio of the value j and j 1 larger by subtracting the value of j ret + (calculated rule Rome )//* / unordered_map<char, int> m; m = { { 'I', 1 },{ 'V', 5 },{ 'X', 10 },{ 'L', 50 },{ 'C', 100 },{ 'D', 500 },{ 'M', 1000 } }; intm = RET [S [s.size () - . 1 ]]; for ( int J = s.size () - 2 ; J> = 0 ; J, ) { int numL = m [S [J]]; int numR m = [S [J + . 1 ]]; IF (numL> = numR) + RET = numL; the else RET - = numL; } return RET; } // string array longest common prefix std ::
String CLeetCode_Solution :: longestCommonPrefix (Vector < String > & STRs) { IF ( 0 == strs.size ()) return ""; string strfirst = strs[0]; for (int i = 1; i < strs.size(); i++) { int j = 0; for (; j < strfirst.size() && j < strs[i].size(); j++) { if (strfirst[j] != strs[i][j]) break; } strfirst = strfirst.substr(0, j); if (strfirst.empty()) return ""; } returnstrfirst; } // determine the validity of the brackets
BOOL CLeetCode_Solution :: isValid ( String S) { // () [] {} IF (s.empty ()) return to true ; IF ( 0 ! = s.size ()% 2 ) return to false ; unordered_map < char , char > m = {{ ' ( ' , ' ) ' }, { ' [ ' , ' ] ' }, { ' { ' , ' }' } }; stack<char> ret; for (int i = 0; i < s.size(); i++) { if (m.find(s[i])!=m.end()) ret.push(m[s[i]]); else if (ret.empty() || ret.top() != s[i]) return false; else ret.pop(); } if (ret.empty()) return true; else return false; }
//合并两个有序链表 ListNode
* CLeetCode_Solution::mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL) return l2; if (l2 == NULL) return l1; if (l1->val <= l2->val) { l1->next = mergeTwoLists(l1->next, l2); return l1; } l2->next = mergeTwoLists(l1, l2->next); return l2; }
//删除排序数组中的重复项
int CLeetCode_Solution::removeDuplicates(vector<int>& nums) { /* if (nums.empty()) return 0; int i = 0; for (int j = 1; j < nums.size();++j) { if (nums[j] != nums[i]) { ++i; nums[i] = nums[j]; } } return i + 1;*/ if (nums.empty()) return 0; int val0 = nums[0]; for (vector<int>::iterator iter = nums.begin()+1; iter != nums.end();) { if (val0 == *iter) { iter = nums.erase(iter); } The else { val0 = * ITER; ITER ++ ; } } return ; nums.size () } // After a given array and a value val nums, you need to remove all values equal to place elements of val, returns removed the new length of the array. int CLeetCode_Solution :: removeElement (Vector < int > the nums &, int Val) { int len = nums.size (); int POS = 0 ; the while (POS < len) { IF (the nums [POS] == Val) the nums [ POS] = the nums [- len]; the else ++ POS; } nums.resize (len); return len; } // handwritten Strstr
int CLeetCode_Solution :: strstr ( String of haystack, String Needle) { int n-haystack.size = (), m = needle.size ();   // reducing cycle, to avoid bounds for ( int I = 0 ; I <-n-m + . 1 ; ++ I) { int J = 0 ; for (; J <m; ++ J) {
       // ensure forward together ha advance
if (of haystack [I + J]! = Needle [J]) BREAK; } if (j == m) return i; } return -1; }

 

Guess you like

Origin www.cnblogs.com/gongkiro/p/12404843.html