- 👋 Hi, I’m @forevermgg
- 👀 I’m interested in Android
- 🌱 I’m currently learning C++
- 💞️ I’m looking to collaborate on ...
- 📫 By https://www.jianshu.com/u/508c24418f06 / [email protected] reach me ...
leetcode's Introduction
leetcode's People
leetcode's Issues
substr
int beautifulBinaryString(string b) {
string temp = "010";
int count = 0;
for (int i = 0 ; (unsigned) i + 3 <= b.size() ; i++) {
string s = b.substr(i,3);
cout << s << endl;
if (s == temp) {
count ++;
i = i + 2;
}
}
return count;
}
最大公约数&最小公倍数
最大公因数
//辗转相除法(欧几里得算法)
int gcd(int a, int b) {
int da = max(a,b);
int xiao = min(a,b);
if(da % xiao == 0)
return xiao;
else
return gcd(xiao, da % xiao);
}
最小公倍数. 两个整数的最小公倍数等于两整数之积除以最大公约数
int lcm(int a, int b) {
return a*b / gcd(a, b);
}
static int gcd(int a, int b){
if(a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b) {
return a*b/gcd(a,b);
}
static long lcm(long a, long b) {
return Math.Abs(a * b) / gcd(a, b);
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
二分法搜索
string twoStrings(string s1, string s2) {
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
int count = 0;
for (uint64_t i = 0; i < s2.length(); i++) {
if (binary_search(s1.begin(), s1.end(), s2[i])) {
count ++;
}
}
return count > 0 ? "YES" : "NO";
}
string twoStrings(string s1, string s2) {
int count = 0;
for (uint64_t i = 0; i < s2.length(); i++) {
if (s1.find(s2[i]) != string::npos) {
count ++;
}
}
return count > 0 ? "YES" : "NO";
}
Time limit exceeded
string twoStrings(string s1, string s2) {
int count = 0;
for (uint64_t i = 0; i < s1.length(); i++) {
for (uint64_t j = 0; j < s2.length(); j++) {
if (s1[i] == s2[j]) {
count ++;
}
}
}
return count > 0 ? "YES" : "NO";
}
字符串反转reverse
find()&&string::npos
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
/*
* Complete the 'minimumNumber' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. STRING password
*/
int minimumNumber(int n, string password) {
string numbers = "0123456789";
string lower_case = "abcdefghijklmnopqrstuvwxyz";
string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string special_characters = "!@#$%^&*()-+";
int a=0,b=0,c=0,d=0;
for(auto s:password){
if(numbers.find(s)!=string::npos){
a=1;
}
else if(lower_case.find(s)!=string::npos){
b=1;
}
else if(upper_case.find(s)!=string::npos){
c=1;
}
else if(special_characters.find(s)!=string::npos){
d=1;
}
}
return max(6-n,4-a-b-c-d);
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
string password;
getline(cin, password);
int answer = minimumNumber(n, password);
fout << answer << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
std::count_if && isupper
https://www.hackerrank.com/challenges/camelcase/problem?isFullScreen=true
计算字符串某一种类型个数
#include <bits/stdc++.h>
using namespace std;
/*
* Complete the 'camelcase' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/
int camelcase(string s) {
int count = 1;
cout = 1 + std::count_if(s.begin(), s.end(),
[](const auto& c) {
return isupper(c);
}
) ;
return count;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s;
getline(cin, s);
int result = camelcase(s);
fout << result << "\n";
fout.close();
return 0;
}
stringstream
stringstream 字符串结束符号
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<int> parseInts(string str) {
// Complete this function
vector<int> result;
stringstream ss(str);
char ch;
int a;
while (ss) {
ss >> a >> ch;
result.push_back(a);
}
return result;
}
int main() {
string str;
cin >> str;
vector<int> integers = parseInts(str);
for(int i = 0; i < integers.size(); i++) {
cout << integers[i] << "\n";
}
return 0;
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.