-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.h
More file actions
73 lines (65 loc) · 1.97 KB
/
36.h
File metadata and controls
73 lines (65 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* @Author: FreedomLy
* @Date: 2018-04-28 14:15:14
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-04-28 14:37:17
* 题目
* 在字符串中找出第一个只出现一次的字符,并返回它的位置
*/
// 思路
// 1. 可以采用一个map来存储每个字符出现的次数,然后再遍历字符串
// 是判断其出现的次数
// 2. 将字符串的每个字符存储到vector中,遍历数组,删除当前字符,然后
// 在剩余字符中查找是否还有该字符,如果没有,就表示是第一个只出现
// 一次的字符
#pragma once
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>
using std::unordered_map;
using std::vector;
using std::string;
using std::pair;
class Solution36 {
public:
int first_unique_char(const string& str)
{
return first_unique_map(str);
// return first_unique_vec(str);
}
private:
// 采用map
int first_unique_map(const string& str)
{
// 使用pair<int, int>来存储出现的次数以及下标
// 可以减少第二次遍历的次数
unordered_map<char, pair<int, int>> m;
for (size_t i = 0; i < str.size(); ++i)
{
m[str[i]].first++;
m[str[i]].second = i;
}
int index = str.size(); // 记录只出现一次的字符的下标
for (auto &p : m)
{
if (p.second.first == 1)
index = std::min(index, p.second.second);
}
return index == static_cast<int>(str.size()) ? -1 : index;
}
// 采用vector
int first_unique_vec(const string& str)
{
vector<char> vc(str.begin(), str.end());
for (size_t i = 0; i < vc.size(); ++i)
{
char tmp = vc[i];
vc.erase(vc.begin() + i);
if (std::find(vc.begin(), vc.end(), tmp) == vc.end())
return i;
vc.insert(vc.begin() + i, tmp);
}
return -1;
}
};