标签:
*p != '\0'
*p != 0
#include "stdafx.h" #include <string> #include<iostream> using namespace::std; class Solution { public: int FirstNotRepeatingChar(string str) { if (str.empty()) return -1; int arr[256] = { 0 }; int length = str.length(); for (int i = 0; i < length; i++){ arr[(int)str[i]]++; std::cout << (int)str[i] << endl; } int retVal = 0; for (int i = 0; i < length; i++){ if (arr[(int)str[i]] == 1) break; retVal++; } if (retVal == str.length()){ retVal = -1; } return retVal; } }; int _tmain(int argc, _TCHAR* argv[]) { Solution s; string test = "asdfasdfe"; int result = s.FirstNotRepeatingChar(test); return 0; }
#include "stdafx.h" #include <string> #include<iostream> using namespace::std; class Solution { public: int FirstNotRepeatingChar(string str) { if (str.empty()) return -1; const char* p = str.data(); int strLength = 0; int arr[256] = { 0 }; while (*p != '\0'){ arr[*p]++; p++; strLength++; } const char* p2 = str.c_str(); int retVal = 0; while (*p2 != '\0'){ if (arr[*p2] == 1) break; p2++; retVal++; } if (retVal == strLength){ retVal = -1; } return retVal; } }; int _tmain(int argc, _TCHAR* argv[]) { Solution s; string test = "asdfasdfe"; int result = s.FirstNotRepeatingChar(test); return 0; }说说这里面的坑:
const char* p = str.data();写成
char* p = str.data();是通不过编译的。
标签:
原文地址:http://blog.csdn.net/chengonghao/article/details/51366020