转栈前提
我是本科读的软件工程,但是因为进了公司的岗位必须使用c语言编程,所以被迫携号转网,说实话,还有机会的话,希望未来还是能从事java开发(希望可以打脸)
碰到的困难
环境适应
之前都是笔记本,现在是pc,必须内网办公。
随手小练
第一天上来给了个任务,无重复字符的最长子串,看了一下,大概可能需要滑动窗口的算法来进行编写。
无重复字符的最长子串
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: s = “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: s = “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
提示:
0 <= s.length <= 5 * 104
s 由英文字母、数字、符号和空格组成
算法难度上,基本上没有,但是难的就是输入输出方式的一些转变。
编译器安装
编译器,vscode,因为开源(公司有钱但是在正版软件购买上比较抠门)。
公司是内网办公,所以用的是gcc的一个打包好的rar包。
- 注意rar包需要解压后,直接将bin添加到电脑的环境变量下
输入输出转变
之前如果在本地编程,使用idea,自动导入
import java.util.*;
现在的话,c记得输入输出,层次结构等等方式都不太一样。
以下代码是输入输出获得未知长度字符串的一个代码块。
代码块
#include<stdio.h>
#include<string.h>
//1:无重复字符的最长子串
//给定一个字符串 s ,请你找出其中不含有重复字符的 **最长子串** 的长度。
char* getString();
char* getS();
int maxLenString(char* ch);
int isRepeat(char* ch,int left,int right);
//the entry of the algorithm
int main(){
char* s=getS();
int res=maxLenString(s);
printf("%d",res);
return 0;
}
//api for get the max length of the string
//use Sliding Window to reduce complexity
int maxLenString(char* ch){
int leng=strlen(ch);
int i;
int j;
int maxL=1;
for(i=0;i<leng;i++){
for(j=i+1;j<leng;j++){
if(isRepeat(ch,i,j)==1){
if(maxL<(j-i+1)){
maxL=j-i+1;
}
}
//Sliding Window
else{
i=j;
break;
}
}
}
return maxL;
}
//get a string from the terminal
char* getS(){
char s[520];
scanf("%s",s);
return &s[0];
}
//return:0 means repeated;1 means not
//judge the range[left,right] has the repeated letter or not
int isRepeat(char* ch,int left,int right){
int leng=right-left+1;
char str[leng];
int i;
int j;
for(i=left;i<=right;i++){
str[i-left]=ch[i];
}
for(i=0;i<leng;i++){
for(j=i+1;j<leng;j++){
if(str[i]==str[j]){
return 0;
}
else if(str[i]>str[j]){
char c=str[i];str[i]=str[j];str[j]=c;
}
}
}
for(i=0;i<leng-1;i++){
if(str[i]==str[i+1]){
return 0;
}
}
printf("right range:%d %d\n",left,right);
return 1;
}
暂时第一天就想到这么多。