mirror of
https://github.com/Estom/notes.git
synced 2026-02-03 02:23:31 +08:00
123
This commit is contained in:
51
code_segment/huawei.cpp
Normal file
51
code_segment/huawei.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
vector<vector<int>> gameOfLife(vector<vector<int>> board) {
|
||||
vector<vector<int>> res(board.size(),vector<int>(board[0].size(),0));
|
||||
for(int i=0;i<board.size();i++){
|
||||
for(int j=0;j<board[0].size();j++){
|
||||
int count =0;
|
||||
for(int k=0;k<9;k++){
|
||||
int x = i-1+k/3;
|
||||
int y = j-1+k%3;
|
||||
if(x==i&&y==j){
|
||||
continue;
|
||||
}
|
||||
if(x>=0 && x<board.size() && y>=0 && y<board[0].size()&&board[x][y]==1){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if(count<2 ||count>3){
|
||||
res[i][j]=0;
|
||||
}
|
||||
else if(count==3){
|
||||
res[i][j]=1;
|
||||
}
|
||||
else if(count==2 && board[i][j]==1){
|
||||
res[i][j]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(){
|
||||
vector<vector<int>> vec = {
|
||||
{0,1,0},
|
||||
{0,0,1},
|
||||
{1,1,1},
|
||||
{0,0,0}
|
||||
};
|
||||
vector<vector<int>> res= gameOfLife(vec);
|
||||
for(auto a:res){
|
||||
for(auto b:a){
|
||||
cout<<b<<" ";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
}
|
||||
39
code_segment/huawei.md
Normal file
39
code_segment/huawei.md
Normal file
@@ -0,0 +1,39 @@
|
||||
给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1 即为活细胞(live),或 0 即为死细胞(dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:
|
||||
|
||||
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
|
||||
如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
|
||||
如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
|
||||
如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
|
||||
下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 m x n 网格面板 board 的当前状态,返回下一个状态。
|
||||
|
||||
示例 1:
|
||||

|
||||
|
||||
输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
|
||||
输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
|
||||
示例 2:
|
||||
|
||||
|
||||
输入:board = [[1,1],[1,0]]
|
||||
输出:[[1,1],[1,1]]
|
||||
|
||||
|
||||
提示:
|
||||
|
||||
m == board.length
|
||||
n == board[i].length
|
||||
1 <= m, n <= 25
|
||||
board[i][j] 为 0 或 1
|
||||
|
||||
进阶:
|
||||
|
||||
你可以使用原地算法解决本题吗?请注意,面板上所有格子需要同时被更新:你不能先更新某些格子,然后使用它们的更新后的值再更新其他格子。
|
||||
本题中,我们使用二维数组来表示面板。原则上,面板是无限的,但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题?
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public void gameOfLife(int[][] board) {
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
40
code_segment/huawei2.cpp
Normal file
40
code_segment/huawei2.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
|
||||
|
||||
string get(string s,vector<string> dictionary){
|
||||
vector<int> vec(dictionary.size(),0);
|
||||
for(int i=0;i<s.size();i++){
|
||||
for(int j=0;j<dictionary.size();j++){
|
||||
// cout<<dictionary[j][vec[j]] << " "<< s[i]<<endl;
|
||||
if(vec[j]<dictionary[j].size() && dictionary[j][vec[j]]==s[i]){
|
||||
vec[j]++;
|
||||
}
|
||||
// else if(vec[j]==dictionary[j].size())
|
||||
}
|
||||
}
|
||||
int max_length =0;
|
||||
int index=-1;
|
||||
for(int i=0;i<s.size();i++){
|
||||
if(vec[i]==dictionary[i].size() && vec[i]>max_length){
|
||||
index=i;
|
||||
max_length=dictionary[i].size();
|
||||
}
|
||||
}
|
||||
if(max_length==0){
|
||||
return "";
|
||||
}
|
||||
return dictionary[index];
|
||||
}
|
||||
int main(){
|
||||
string s = "abpcplea";
|
||||
vector<string> dictionary = {"ale","apple","monkey","plea"};
|
||||
// vector<string> dictionary = {"a","b","c"};
|
||||
|
||||
sort(dictionary.begin(), dictionary.end());
|
||||
string res = get(s,dictionary);
|
||||
cout<<res<<endl;
|
||||
return 0;
|
||||
}
|
||||
27
code_segment/huawei2.md
Normal file
27
code_segment/huawei2.md
Normal file
@@ -0,0 +1,27 @@
|
||||
给你一个字符串 s 和一个字符串数组 dictionary 作为字典,找出并返回字典中最长的字符串,该字符串可以通过删除 s 中的某些字符得到。
|
||||
|
||||
如果答案不止一个,返回长度最长且字典序最小的字符串。如果答案不存在,则返回空字符串。
|
||||
|
||||
示例 1:
|
||||
|
||||
输入:s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
|
||||
输出:"apple"
|
||||
示例 2:
|
||||
|
||||
输入:s = "abpcplea", dictionary = ["a","b","c"]
|
||||
输出:"a"
|
||||
|
||||
提示:
|
||||
|
||||
1 <= s.length <= 1000
|
||||
1 <= dictionary.length <= 1000
|
||||
1 <= dictionary[i].length <= 1000
|
||||
s 和 dictionary[i] 仅由小写英文字母组成
|
||||
|
||||
|
||||
|
||||
class Solution {
|
||||
public String findLongestWord(String s, List<String> dictionary) {
|
||||
|
||||
}
|
||||
}
|
||||
BIN
code_segment/image/2021-09-26-10-33-16.png
Normal file
BIN
code_segment/image/2021-09-26-10-33-16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 67 KiB |
0
code_segment/wangyi.cpp
Normal file
0
code_segment/wangyi.cpp
Normal file
@@ -1,9 +1,5 @@
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
/**
|
||||
* 交换任意两数的本质是改变了元素位置,
|
||||
* 故建立元素与其目标状态应放置位置的映射关系
|
||||
*/
|
||||
int getMinSwaps(vector<int> v)
|
||||
{
|
||||
vector<int> v1(v); //将A内元素复制到B。
|
||||
@@ -42,6 +38,6 @@ int main()
|
||||
v.push_back(k);
|
||||
}
|
||||
int num = getMinSwaps(v);
|
||||
cout<<"交换次数:"<<num<<endl;
|
||||
cout<<num<<endl;
|
||||
return 0;
|
||||
}
|
||||
90
code_segment/xiaohongshu2.cpp
Normal file
90
code_segment/xiaohongshu2.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include<vector>
|
||||
#include<iostream>
|
||||
#include<sstream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int n,m;
|
||||
cin>>n>>m;
|
||||
vector<vector<int>> vec(n,vector<int>(m,0));
|
||||
int x,y,d=0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=0;j<m;j++){
|
||||
char c = cin.get();
|
||||
if(c=='B'){
|
||||
vec[i][j]=0;
|
||||
}
|
||||
else if(c=='O'){
|
||||
vec[i][j]=1;
|
||||
}
|
||||
else if(c=='R'){
|
||||
vec[i][j]=2;
|
||||
x=i,y=j;
|
||||
}
|
||||
else if(c=='\n'){
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
int num=0;
|
||||
cin>>num;
|
||||
cin.ignore();
|
||||
int ox=x,oy=y;
|
||||
while(num--){
|
||||
string line;
|
||||
getline(cin,line);
|
||||
if(line=="Turn right"){
|
||||
d=(d+1)%4;
|
||||
}
|
||||
else if(line=="Turn left"){
|
||||
d=((d-1)+4)%4;
|
||||
}
|
||||
else if(line[0]=='F'){
|
||||
istringstream is(line);
|
||||
string temp;
|
||||
is>>temp;
|
||||
int step;
|
||||
is>>step;
|
||||
|
||||
while(step--){
|
||||
if(d==0){
|
||||
if(x-1>=0 && vec[x-1][y]==0){
|
||||
x=x-1;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(d==1){
|
||||
if(y+1<m && vec[x][y+1]==0){
|
||||
y = y+1;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(d==2){
|
||||
if(x+1<n && vec[x+1][y]==0){
|
||||
x=x+1;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(d==3){
|
||||
if(y-1>0 && vec[x][y-1]==0){
|
||||
y=y-1;
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
cout<<x-ox<<y-oy<<endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user