字符串

#include<iostream>
#include<string>
using namespace std;

class Solution
{
public:
    string rotateString(string A,int offset)
    {
        if(A.empty() || A.size() == 0)
        {
            return A;
        }
        
        int len = A.size();
        offset %= len;
        reverse(A,0,len - offset - 1);
        reverse(A,len - offset,len - 1);
        reverse(A,0,len - 1);
    }
private:
    void reverse(string &str,int start,int end)
    {
        while(start < end)
        {
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
    }
};

1、Determine if all chararters of a string are unique.

一般来说,一旦出现“unique”,就落入使用hash table 或者bitset来判断元素出现与否的范畴。

#include <iostream>
#include<bitset>
#include<string>

using namespace std;


bool isUnique(string input)
{
    bitset<256> hashMap;
    for(int index=0;index<input.length();index++)
    {
        if(hashMap[(int)input[index]])
            return false;
        hashMap.set((int)input[index]);
    }
    return true;
}

int main()
{
    string input;
    while(1)
    {
        cin >> input;
        if(isUnique(input))
            cout << "The string is unique! " << input << endl;
        else
            cout << "The string not!!" << input << endl;
    }
    return 0;
}

2、Given two strings,determine if they are permutations of each other.

解法1:置换的特性:无论如何变化,每个字符出现的次数一定相同。一旦需要统计一个元素集中元素出现的次数,我们就应该想到hash table.

解法2:对每个string中的字符安装ASCII编码顺序进行排序。如果是一个置换,那么排序完的两个string应该相等。

#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;

bool isPermutation(string stringA,string stringB)
{
    if(stringA.length() != stringB.length())
        return false;

    unordered_map<char,int> hashMapA;
    unordered_map<char,int> hashMapB;
    for(int i=0;i<stringA.length();i++)
    {
        hashMapA[stringA[i]]++;
        hashMapB[stringB[i]]++;
    }

    if(hashMapA.size() != hashMapB.size())
        return false;

    unordered_map<char,int>::iterator it;
    for(it=hashMapA.begin();it!=hashMapA.end();it++)
    {
        if(it->second != hashMapB[it->first])
            return false;
    }
    return true;
}

int main()
{
    string input1,input2;
    while(1)
    {
        cin >> input1;
        cin >> input2;
        if(isPermutation(input1,input2))
            cout << "字符串置换! " << endl;
        else
            cout << "The string not!!" << endl;
    }
    return 0;
}
3、Given a newspaper and message as two strings,check if the message can be composed using letters in the newspaper.

解题分析:message 中用到的字符必须出现在newspaper中。汽车,message中任意字符出现的次数一定少于其在newspaper中出现的次数。统计一个元素集中元素出现的次数,我们就应该想到hash table.

#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;

bool canCompose(string newspaper,string message)
{
    unordered_map<char,int> hashMap;
    int i;
    if(newspaper.length() < message.length())
        return false;

    for(i=0;i<newspaper.length();i++)
    {
        hashMap[newspaper[i]]++;
    }

    for(i=0;i<message.length();i++)
    {
        if(hashMap.count(message[i]) == 0)
            return false;
        if(--hashMap[message[i]] < 0)
            return false;
    }
    return true;
}

int main()
{
    string input1,input2;
    while(1)
    {
        cin >> input1;
        cin >> input2;
        if(canCompose(input1,input2))
            cout << "字符串input1包含字符串input2! " << endl;
        else
            cout << "The string not!!" << endl;
    }
    return 0;
}
4、Anagram

Write a method anagram(s,t) to decide if two strings are anagrams or not.

Example

Given s = "abcd",t="dcab",return true.

解题思路:1:hash;2:sort

#include<iostream>
#include<string>
using namespace std;
bool anagram(string s,string t)
{
    if(s.empty() || t.empty())
        return false;
    if(s.size() != t.size())
        return false;

    int letterCount[256] = {0};

    for(int i=0;i != s.size();i++)
    {
        ++letterCount[s[i]];
        --letterCount[t[i]];
    }

    for(int i=0;i != t.size();++i)
    {
        if(letterCount[t[i]] < 0)
            return false;
    }
    return true;
}
bool anagram2(string s,string t)//sort
{
    if(s.empty() || t.empty())
        return false;
    if(s.size() != t.size())
        return false;


    sort(s.begin(),s.end());
    sort(t.begin(),t.end());


    if(s == t)
        return true;
    else
        return false;
}
int main()
{
    string input1,input2;
    while(1)
    {
        cin >> input1;
        cin >> input2;
        if(anagram(input1,input2))
            cout << " 为真! " << endl;
        else
            cout << "The string not!!" << endl;
    }
    return 0;
}

5、Find a pair of two elements in an array,whose sum is a given target number.

最直观的的方法是再次扫描数组,判断target - array[i]是否存在数组中,但效率不高;

如何保存之前的处理结果?可以使用hash table “target  - array[i]” 是否存在数组中。

#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

vector<int> addToTarget(vector<int> &numbers,int target)
{
    unordered_map<int,int> numToIndex;
    vector<int> vi(2);
    for(auto it=numbers.begin();it!=numbers.end();it++)
    {
        if(numToIndex.count(target - *it))
        {
            vi[0] = numToIndex[target - *it] + 1;
            vi[1] = (int)(it - numbers.begin()) + 1;
            return vi;
        }
        numToIndex[*it] = (int)(it - numbers.begin());
    }
}


int main()
{
    int a[] = {1,10,2,10,7,1,3,7,3,10};
    vector<int> temp(a,a+10);
    vector<int> index(2);

    index = addToTarget(temp,17);
    if(index[0] == 0)
        cout << "数组中没有两项相加等于target" << endl;
    else
        cout << index[0] << "  " << index[1] << endl;
    return 0;
}

6、Longest Common Substring

Given two strings,find the longest common substring.Return the length of it.

Example

Given A = "ABCD",B="CBCE",return 2.

#include<iostream>
#include<string>
using namespace std;

int longestCommonSubstring(string &A,string &B)
{
    if(A.empty() || B.empty())
    {
        return 0;
    }

    int lcs = 0,lcs_temp = 0;

    for(int i=0;i<A.size();i++)
    {
        for(int j=0;j<B.size();j++)
        {
            lcs_temp = 0;
            while((i + lcs_temp < A.size()) && (j + lcs_temp < B.size()) && (A[i + lcs_temp] == B[j + lcs_temp]))
            {
                ++lcs_temp;
            }
            if(lcs_temp > lcs)
            {
                lcs = lcs_temp;
            }
        }

    }
    return lcs;
}


int main()
{
    string input1,input2;
    while(1)
    {
        cin >> input1 >> input2;
        cout << longestCommonSubstring(input1,input2) << endl;
    }
    return 0;
}

7、Reverse Words in String

Given input -> "I have 36 books,40 pens2.";output -> "I evah 36 skoob,40 2snep."

#include<iostream>
#include<cstring>
using namespace std;

bool isPunctuationOrSpace(char *character)
{
    return *character == ' ' || *character == ',' || *character == '.';
}

bool isNumber(char *character)
{
    return *character >= '0' && *character <= '9';
}

bool needReverse(char *sentence,int *offset)
{
    int length = (int)strlen(sentence);
    bool needReverse = false;
    *offset = 0;
    while(!isPunctuationOrSpace(sentence + *offset) && (*offset) < length)
    {
        if(!isNumber(sentence + *offset))
        {
            needReverse = true;
        }
        (*offset)++;
    }
    return needReverse;
}

void reverseWord(char *word,int length)
{
    int i = 0,j = length - 1;
    while(i < j)
    {
        swap(*(word + i),*(word + j));
        i++;
        j--;
    }
}

void reverseSentence(char *sentence)
{
    int length = (int)strlen(sentence);
    int offset;
    for(int i=0;i<length;)
    {
        if(needReverse(sentence + i,&offset))
        {
            reverseWord(sentence + i,offset);
        }
        i += (offset + 1);
    }
}

int main()
{
    char temp[100] = {"lihui51 45 yui 36512 l4"};
    reverseSentence(temp);
    cout << temp << endl;
    return 0;
}

8、Rotate String

Given a string and an offset,rotate string by offset.(rotate from left to right)

Example

Given "abcdefg"

for offset = 0,return "abcdefg"

for offset = 1,return "gabcdef"

#include<iostream>
#include<string>
using namespace std;

class Solution
{
public:
    string rotateString(string A,int offset)
    {
        if(A.empty() || A.size() == 0)
        {
            return A;
        }
        
        int len = A.size();
        offset %= len;
        reverse(A,0,len - offset - 1);
        reverse(A,len - offset,len - 1);
        reverse(A,0,len - 1);
    }
private:
    void reverse(string &str,int start,int end)
    {
        while(start < end)
        {
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值