Zero, write in front
This is the 23rd day of clock in. Today's topic is a little difficult. The main knowledge points are
1, Main knowledge points
1. String segmentation
Brother hero's idea is to use a two-dimensional character group to save a new string. I found that it can also be split with a character pointer array, which is simpler. In fact, the idea is very simple,
- When the first encounter is not a split character, save the corresponding address.
- When the delimiter is first encountered, it becomes 0 when it is set to the end
int size = 0;//Record size char *ans[1000];//Record the first address of each string bool flag = true; //Identify whether it is the first time int i = 0; while(s[i] == ' ') i++;//Skip first separator ans[size++] = &s[i];//Insert first string for(;s[i];++i){ //Continue finding splits if(s[i] == ' '&&flag = true){//Separator encountered for the first time s[i] = 0; flag = true; } if(s[i] != 0 &&flag){ //Non delimiter encountered for the first time flag = false; ans[size]++ = &s[i]; } }
2, After class exercises
58. Length of the last word
58. Length of the last wordhttps://leetcode-cn.com/problems/length-of-last-word/
thinking
Save the position of the last word according to the separator, modify the following to the terminator, and return the length of the last word.
int lengthOfLastWord(char * s){ int ans; bool flag = true; for(int i = 0;s[i];++i){ if(s[i] == ' '){ //Here is to dry all the blanks to 0 s[i] = 0; flag = true; } if(s[i] != 0 &&flag){//First, this encountered a non delimiter. Split save flag = false; ans = i; //The first character of the last word is saved } } return strlen(&s[ans]);//Returns the length of the last word }
Result analysis
Are you satisfied?
434. Number of words in string
434. Number of words in stringhttps://leetcode-cn.com/problems/number-of-segments-in-a-string/
thinking
After splitting, just return the number?
int countSegments(char * s){ bool flag = true; int ans = 0; //Number of Statistics for(int i = 0;s[i];++i){ if(s[i] == ' '){//Separator split encountered s[i] = 0; flag = true; } if(s[i] != 0 &&flag){//Non separator encountered for the first time, statistics flag = false; ans ++; } } return ans; }
Result analysis
All right
2042. Check whether the numbers in the sentence are incremented
thinking
Save the value of each time, and then compare whether the value you see this time is greater than last time?
bool areNumbersAscending(char * s){ int temp,i = 0; while(s[i] && (s[i]<'0' || s[i] >'9')) i++;//Not number skip temp = s[i] - '0'; //Save the initial value of temp for comparison if(s[i+1] >= '0' && s[i+1] <= '9'){//Save temp initial value temp *= 10; temp += (s[++i] - '0'); } for(i++ ;s[i]; ++i) //Start scanning if(s[i] >= '0' && s[i] <= '9'){//Sweep to number int temp2 = s[i] - '0'; //Record the current value if(s[i + 1] >= '0' && s[i + 1] <= '9'){ temp2 *= 10; temp2 += (s[++i] - '0'); } if(temp2 <= temp)//Compare the unqualified items and throw them out return false; else temp = temp2; //Match start next comparison } return true; }
Result analysis
Make do with
2047. Number of valid words in the sentence
thinking
Just follow its requirements after splitting. There's nothing else except trouble. Be careful, be careful, be careful again.
int countValidWords(char * sentence){ char *ans[500];//Create word array int wordsize = 0;//Record size bool flag = true; for(int i = 0;sentence[i];++i){ //Split all words if(sentence[i] == ' '&& flag == false){ flag = true; sentence[i] = 0; } else if(sentence[i] != ' ' && flag == true){ flag = false; ans[wordsize++] = &sentence[i]; } } int res = 0; //Count the final results for(int i = 0;i < wordsize;++i){ int num = 0,zimu = 0,lianzifu = 0,biaodian = 0,j = 0; //Count the number of all types of things for(j = 0;ans[i][j];++j){ if(ans[i][j] >= '0' && ans[i][j] <= '9') num++; else if(ans[i][j] >= 'a' && ans[i][j] <= 'z') zimu++; else if(ans[i][j] == '-') lianzifu++; else if(ans[i][j] == '!'||ans[i][j] == '.'||ans[i][j] == ',') biaodian++; } if(num) continue;//Continue with letters if(lianzifu > 1||(lianzifu == 1 && (ans[i][0] == '-' || ans[i][j-1] == '-'))) continue;//The hyphen contains more than one or only one character, but the last and first characters cannot appear if(lianzifu == 1 && biaodian == 1 && (ans[i][j-1] == '!'||ans[i][j-1] == '.'||ans[i][j-1] == ',') && ans[i][j-2] == '-') continue; //There are hyphens and punctuation, and the hyphen is the first or penultimate if(biaodian >1 ||(biaodian == 1 && (!(ans[i][j-1] == '!'||ans[i][j-1] == '.'||ans[i][j-1] == ',')))) continue;//Punctuation is not at the end printf("%d ",i); res++; } return res; }
Result analysis
satisfied
1816. Truncate sentences
1816. Truncate sentenceshttps://leetcode-cn.com/problems/truncate-sentence/
thinking
Split the number of characters, assign a value of 0 directly to the place, and return it.
char * truncateSentence(char * s, int k){ for(int i = 0;s[i];++i) if(s[i] == ' '&&!(--k))//To the place s[i] = 0;//Insert Terminator return s; }
Result analysis
All right
1784. Check binary string field
thinking
The meaning of the question is much more difficult than that of the question. At the beginning, I can't understand it. In fact, if 0 can't appear, then I can't include the 01 string.
bool checkOnesSegment(char * s){ return !strstr(s,"01"); }
Result analysis
OK
468. Verify IP address
468. Verify IP addresshttps://leetcode-cn.com/problems/validate-ip-address/
thinking
It's not difficult, but it's really hard. It's too hard.
- Determine whether the delimiter is 3 points or 7 colons
- Judge whether it is IPv4 or IPv6 respectively
- return
char answer[3][10] = {"IPv4","IPv6","Neither"}; int validIPv4(char **); int validIPv6(char **); char * validIPAddress(char * queryIP){ char *cou[10]; cou[0] = queryIP; int dot = 1,colons = 1; for(int i = 0;queryIP[i];++i) //Split string if(queryIP[i] == '.'){ if(dot + colons > 9) return answer[2];//Prevent overflow cou[dot++] = &queryIP[i+1]; queryIP[i] = 0; } else if(queryIP[i] == ':'){ if(dot + colons > 9) return answer[2];//Prevent overflow cou[colons++] = &queryIP[i+1]; queryIP[i] = 0; } if(dot == 4 && colons == 1) return answer[validIPv4(cou)]; //3 points if(colons == 8 && dot == 1) return answer[validIPv6(cou)]; //7 colons return answer[2]; //Nothing } int validIPv4(char **cou){ for(int i = 0;i < 4;i++){ int temp = 0; if(!cou[i][0]) return 2; //Empty group for(int j = 0;cou[i][j];j++){ if(cou[i][j] < '0' ||cou[i][j] > '9') return 2; //Contains non numeric non-conforming returns temp *= 10; temp += cou[i][j] - '0'; if(temp > 255||temp < 0) return 2; //Non conformance } if(temp == 0 && cou[i][1]) return 2;//More than 0 else if(temp !=0 && cou[i][0] == '0') return 2;//0 more head } return 0; //normal } int validIPv6(char **cou){ for(int i = 0;i < 8;++i){ if(!cou[i][0]) return 2; //The empty group does not meet the requirements int size = 0; //Statistics array size for(;cou[i][size];size++){ //If not the given character if(!((cou[i][size] >='0'&&cou[i][size] <='9')||(cou[i][size] >='a'&&cou[i][size] <='f')||(cou[i][size] >='A'&&cou[i][size] <='F'))) return 2; } if(size > 4) return 2; //If more than 4 digits } return 1; }
Result analysis
All right
Write at the end
I'm so grumpy recently. Why do some people always dislike learning and bother others to learn? I don't want anyone to help me. As long as it doesn't hinder me from doing things, is it so difficult? Alas, cry haw.
When I drive the tablet and can run win10, I'll die in the library!