Skip to content

Commit f474ed6

Browse files
committed
docs: 柠檬微趣笔试题(#818)
close #818 * rename 周报 Signed-off-by: LetMeFly666 <[email protected]>
1 parent 56738b0 commit f474ed6

23 files changed

+1131
-45
lines changed

.commitmsg

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 23:04:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 23:33:11
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
void nextPermutation(vector<int>& nums) {
14+
int i = nums.size() - 1;
15+
for (; i > 0; i--) {
16+
if (nums[i] > nums[i - 1]) {
17+
break;
18+
}
19+
}
20+
cout << i << endl;
21+
if (!i) {
22+
reverse(nums.begin(), nums.end());
23+
return;
24+
}
25+
i--;
26+
int j = nums.size();
27+
while (nums[--j] <= nums[i]) {
28+
cout << j << (nums[j] <= nums[i]) << endl;
29+
};
30+
swap(nums[i], nums[j]);
31+
reverse(nums.begin() + i + 1, nums.end());
32+
}
33+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 22:52:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 22:53:13
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
vector<string> s2v(string s) {
14+
vector<string> ans;
15+
int last = -1;
16+
for (int i = 0; i <= s.size(); i++) {
17+
if (i == s.size() || s[i] == ' ') {
18+
string thisStr = s.substr(last + 1, i - last - 1);
19+
if (thisStr.size()) {
20+
ans.push_back(thisStr);
21+
}
22+
last = i;
23+
}
24+
}
25+
return ans;
26+
}
27+
public:
28+
/**
29+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
30+
*
31+
*
32+
* @param pattern string字符串
33+
* @param str string字符串
34+
* @return bool布尔型
35+
*/
36+
bool wordPattern(string pattern, string str) {
37+
// write code here
38+
unordered_map<char, string> p2s;
39+
unordered_map<string, char> s2p;
40+
vector<string> v = s2v(str);
41+
if (pattern.size() != v.size()) {
42+
return false;
43+
}
44+
for (int i = 0; i < v.size(); i++) {
45+
if (p2s.count(pattern[i])) {
46+
if (p2s[pattern[i]] != v[i]) {
47+
return false;
48+
}
49+
} else {
50+
p2s[pattern[i]] = v[i];
51+
}
52+
53+
if (s2p.count(v[i])) {
54+
if (s2p[v[i]] != pattern[i]) {
55+
return false;
56+
}
57+
} else {
58+
s2p[v[i]] = pattern[i];
59+
}
60+
}
61+
return true;
62+
}
63+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 20:52:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 20:52:36
6+
*/
7+
#include <unordered_map>
8+
class Solution {
9+
private:
10+
vector<string> s2v(string s) {
11+
vector<string> ans;
12+
int last = -1;
13+
for (int i = 0; i <= s.size(); i++) {
14+
if (i == s.size() || s[i] == ' ') {
15+
string thisStr = s.substr(last + 1, i - last - 1);
16+
if (thisStr.size()) {
17+
ans.push_back(thisStr);
18+
}
19+
last = i;
20+
}
21+
}
22+
return ans;
23+
}
24+
public:
25+
/**
26+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
27+
*
28+
*
29+
* @param pattern string字符串
30+
* @param str string字符串
31+
* @return bool布尔型
32+
*/
33+
bool testMatch(string pattern, string str) {
34+
// write code here
35+
unordered_map<char, string> ma;
36+
vector<string> v = s2v(str);
37+
if (pattern.size() != v.size()) {
38+
return false;
39+
}
40+
for (int i = 0; i < v.size(); i++) {
41+
if (ma.count(pattern[i])) {
42+
if (ma[pattern[i]] != v[i]) {
43+
return false;
44+
}
45+
} else {
46+
ma[pattern[i]] = v[i];
47+
}
48+
}
49+
return true;
50+
}
51+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 21:01:38
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 23:43:37
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
private:
14+
string itos(int n) {
15+
string s;
16+
while (n) {
17+
s = char('0' + n % 10) + s;
18+
n /= 10;
19+
}
20+
return s;
21+
}
22+
23+
ll stoi(string s) {
24+
ll ans = 0;
25+
for (char c : s) {
26+
ans = ans * 10 + c - '0';
27+
}
28+
return ans;
29+
}
30+
public:
31+
/**
32+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
33+
*
34+
*
35+
* @param N int整型
36+
* @return int整型
37+
38+
12334
39+
12343
40+
41+
12343
42+
12433
43+
44+
1235
45+
*/
46+
int nextGreaterElement(int N) {
47+
// write code here
48+
string s = itos(N);
49+
int last = -1;
50+
for (int i = 1; i < s.size(); i++) {
51+
if (s[i] > s[i - 1]) {
52+
last = i - 1;
53+
}
54+
}
55+
if (last == -1) {
56+
return -1;
57+
}
58+
cout << last << endl;
59+
int j = s.size() - 1;
60+
for (; ; j--) {
61+
if (s[j] > s[last]) {
62+
break;
63+
}
64+
}
65+
swap(s[last], s[j]);
66+
// cout << s << endl;
67+
reverse(s.begin() + last + 1, s.end());
68+
// cout << s << endl;
69+
ll ans = stoi(s);
70+
cout << ans << endl;
71+
if (ans > INT_MAX) {
72+
return -1;
73+
}
74+
return ans;
75+
}
76+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 21:01:38
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 21:01:59
6+
*/
7+
#include <string>
8+
typedef long long ll;
9+
class Solution {
10+
private:
11+
string itos(int n) {
12+
string s;
13+
while (n) {
14+
s = char('0' + n % 10) + s;
15+
n /= 10;
16+
}
17+
return s;
18+
}
19+
20+
ll stoi(string s) {
21+
ll ans = 0;
22+
for (char c : s) {
23+
ans = ans * 10 + c - '0';
24+
}
25+
return ans;
26+
}
27+
public:
28+
/**
29+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
30+
*
31+
*
32+
* @param N int整型
33+
* @return int整型
34+
35+
12334
36+
12343
37+
38+
12343
39+
12433
40+
41+
1235
42+
*/
43+
int nextGreaterElement(int N) {
44+
// write code here
45+
string s = itos(N);
46+
int last = -1;
47+
for (int i = 1; i < s.size(); i++) {
48+
if (s[i] > s[i - 1]) {
49+
last = i;
50+
}
51+
}
52+
if (last == -1) {
53+
return -1;
54+
}
55+
swap(s[last], s[last - 1]);
56+
// cout << s << endl;
57+
sort(s.begin() + last, s.end());
58+
// cout << s << endl;
59+
ll ans = stoi(s);
60+
cout << ans << endl;
61+
if (ans > INT_MAX) {
62+
return -1;
63+
}
64+
return ans;
65+
}
66+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-16 20:53:22
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-16 20:53:22
6+
*/
7+
#include <string>
8+
class Solution {
9+
private:
10+
string itos(int n) {
11+
string s;
12+
while (n) {
13+
s = char('a' + n % 10) + s;
14+
n /= 10;
15+
}
16+
return s;
17+
}
18+
19+
int stoi(string s) {
20+
int ans = 0;
21+
for (char c : s) {
22+
ans = ans * 10 + c - 'a';
23+
}
24+
return ans;
25+
}
26+
public:
27+
/**
28+
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
29+
*
30+
*
31+
* @param N int整型
32+
* @return int整型
33+
34+
12334
35+
12343
36+
37+
12343
38+
12433
39+
40+
1235
41+
*/
42+
int findGreaterNum(int N) {
43+
// write code here
44+
string s = itos(N);
45+
int last = -1;
46+
for (int i = 1; i < s.size(); i++) {
47+
if (s[i] > s[i - 1]) {
48+
last = i;
49+
}
50+
}
51+
if (last == -1) {
52+
return -1;
53+
}
54+
swap(s[last], s[last - 1]);
55+
sort(s.begin() + last + 1, s.end());
56+
return stoi(s);
57+
}
58+
};

0 commit comments

Comments
 (0)