careercup-数组和字符串1.1

简介: 1.1 实现一个算法,确定一个字符串的所有字符是否全部不同。假设不允许使用额外的数据结构,又该如何处理? C++实现: #include #include #include using namespace std; /* 判断是否有重复字符 */ bool unqString(string s) { if(s.

1.1 实现一个算法,确定一个字符串的所有字符是否全部不同。假设不允许使用额外的数据结构,又该如何处理?

C++实现:

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

/*
判断是否有重复字符
*/
bool unqString(string s)
{
    if(s.empty())
        return true;
    int n=s.length();
    int word[256]={0};
    int i;
    for(i=0;i<n;i++)
        word[s[i]]++;
    for(i=0;i<n;i++)
        if(word[s[i]]>1)
            return false;
    return true;
}
int main()
{
    string str="asdhfdsf";
    cout<<unqString(str)<<endl;
}

使用关联容器set:

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

bool unqString(string s)
{
    set<char> st;
    if(s.empty())
        return false;
    size_t i;
    for(i=0;i<s.length();i++)
        st.insert(s[i]);
    if(st.size()!=s.length())
        return false;
    else
        return true;
}

int main()
{
    string str="asdhf";
    cout<<unqString(str)<<endl;
}

 

相关文章
|
5天前
字符串——OJ题
字符串——OJ题
52 0
|
5天前
|
机器学习/深度学习 索引
【力扣】387. 字符串中的第一个唯一字符
【力扣】387. 字符串中的第一个唯一字符
|
6月前
|
存储
数组OJ题汇总(一)
数组OJ题汇总(一)
45 0
|
11月前
剑指offer_数组---数组中只出现一次的数字
剑指offer_数组---数组中只出现一次的数字
51 0
LeetCode 5429. 数组中的 k 个最强值
给你一个整数数组 arr 和一个整数 k 。
50 0
AcWing 737. 数组替换
AcWing 737. 数组替换
47 0
AcWing 737. 数组替换
LeetCode 5956. 找出数组中的第一个回文字符串
LeetCode 5956. 找出数组中的第一个回文字符串
126 0
|
算法 C++
careercup-数组和字符串1.7
1.7 编写一个算法,若M*N矩阵中某个元素为0,则将其所在的行与列清零。 类似于leetcode中的 Set Matrix Zeroes   C++实现代码: #include #include using namespace std; void setMatricZero(vector &matrix) { if(matrix.
661 0