1.Algorithm:
来源:力扣(LeetCode)
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: “()”
输出: true
示例 2:
输入: “()[]{}”
输出: true
示例 3:
输入: “(]”
输出: false
示例 4:
输入: “([)]”
输出: false
示例 5:
输入: “{[]}”
输出: true
刚开始使用了下面的解法,思路比较简单,但是代码量比较大:
bool isValid(char * s){
int strlen1 = strlen(s);
if (strlen1==0)
return true;
for(int i =0; i<strlen1; i++)
{
switch(s[i]){
case ')':{
if((i==0)||(s[i-1]!='('))
return false;
else{
for(int j=i+1; j<strlen1; j++){
s[j-2]=s[j];
}
strlen1-=2;
i-=2;
}
break;
}
case '}':{
if((i==0)||(s[i-1]!='{'))
return false;
else{
for(int j=i+1; j<strlen1; j++){
s[j-2]=s[j];
}
strlen1-=2;
i-=2;
}
break;
}
case ']':{
if((i==0)||(s[i-1]!='['))
return false;
else{
for(int j=i+1; j<strlen1; j++){
s[j-2]=s[j];
}
strlen1-=2;
i-=2;
}
break;
}
default:
break;
}
}
if(strlen1==0) return true;
return false;
}
又试着使用栈的方法去解决,方法如下:
#define NUM_MAX 10000
typedef struct myStack{
char s[NUM_MAX];
int top;
}myStack_s;
myStack_s * myStackInit(){
myStack_s *mystack;
mystack = (myStack_s *)malloc(sizeof(myStack_s));
if(NULL == mystack){
return NULL;
}
mystack->top = -1;
return mystack;
}
void myStackFree(myStack_s * mystack){
free(mystack);
}
int myStackGetTopIndex(myStack_s *mystack){
return mystack->top;
}
void myStackPush(myStack_s *mystack, char cValue){
if(mystack->top<NUM_MAX){
mystack->top++;
mystack->s[mystack->top] = cValue;
}
}
bool myStackPop(myStack_s *mystack, char *cValue){
bool bIsValue = false;
if(mystack->top >= 0){
*cValue = mystack->s[mystack->top];
mystack->top--;
bIsValue = true;
}
return bIsValue;
}
bool isValid(char * s){
if(strlen(s) == 0){
return true;
}
char cValue;
bool bIsValid = true;
myStack_s *mystack = myStackInit();
if(NULL == mystack){
return false;
}
for(int i = 0; i < strlen(s); i++){
if((s[i] == '(') || (s[i] == '[') || (s[i] == '{')){
myStackPush(mystack, s[i]);
}
else{
if(false == myStackPop(mystack, &cValue)){
bIsValid = false;
break;
}
if(!(((s[i] == ')') && ('(' == cValue)) ||
((s[i] == ']') && ('[' == cValue)) ||
((s[i] == '}') && ('{' == cValue))))
{
bIsValid = false;
}
}
}
if ((true ==bIsValid) &&(myStackGetTopIndex(mystack) != -1)){
bIsValid = false;
}
myStackFree(mystack);
return bIsValid;
}
用栈实现的时候,出了错误,发现在代码里怀疑的地方使用printf打印怀疑的变量,leetcode都能显示 出来,方便调试代码,点个赞,耗时4ms,又查看来更好的解决办法,有的使用malloc方法,有的使用数组,使用malloc方法的在申请内存时没有判断是否申请成功,且在内部流程return 退出时,没有释放内存,感觉还是下面的这个数组的比较好一些。
bool isValid(char * s){
char stack[10000];
int index_s,index_stack;
index_s=index_stack=0;
if(s[0]==' '){
return false;
}
while(s[index_s]!='\0'){
if(s[index_s]=='('||s[index_s]=='['||s[index_s]=='{'){
index_stack++;
stack[index_stack]=s[index_s];
}
else{
if(index_stack==0){
return false;
}
else{
if(stack[index_stack]=='('&&s[index_s]!=')'){
return false;
}
if(stack[index_stack]=='['&&s[index_s]!=']'){
return false;
}
if(stack[index_stack]=='{'&&s[index_s]!='}'){
return false;
}
index_stack--;
}
}
index_s++;
}
if(index_stack!=0){
return false;
}
return true;
}
2.Review:
看左耳听风专栏看到这个google自我评分卡,自我分析了后,发现即使用了最长的C语言,也就处于4-5之间。。。。
0 - you are unfamiliar with the subject area.(0 -你不熟悉主题领域。)
1 - you can read / understand the most fundamental aspects of the subject area.(1 -你可以阅读/了解主题领域最基本的方面。)
2 - ability to implement small changes, understand basic principles and able to figure out additional details with minimal help.(2 -能够实现小的变化,理解基本原理,并能在最小的帮助下找出更多的细节。)
3 - basic proficiency in a subject area without relying on help.(3 -在不依赖帮助的情况下,熟练掌握某一科目。)
4 - you are comfortable with the subject area and all routine work on it: (4 -你对主题领域和所有日常工作都很熟悉:)
For software areas - ability to develop medium programs using all basic language features w/o book, awareness of more esoteric features (with book).(对于软件领域来说,能够使用所有基本的语言来开发中等的程序,使用w/o book,了解更深奥的特性(带书)。)
For systems areas - understanding of many fundamentals of networking and systems administration, ability to run a small network of systems including recovery, debugging and nontrivial troubleshooting that relies on the knowledge of internals.(对于系统领域——了解网络和系统管理的许多基础知识,能够运行一个小型的系统网络,包括恢复、调试和依赖于内部知识的重要故障排除。)
5 - an even lower degree of reliance on reference materials. Deeper skills in a field or specific technology in the subject area.(5 -对参考资料的依赖程度更低。在某一领域或某一特定技术领域有较深的技能。)
6 - ability to develop large programs and systems from scratch. Understanding of low level details and internals. Ability to design / deploy most large, distributed systems from scratch.(6 -能够从头开始开发大型程序和系统。了解低层次的细节和内部信息。能够设计/部署大多数大型的分布式系统。)
7 - you understand and make use of most lesser known language features, technologies, and associated internals. Ability to automate significant amounts of systems administration.(7 -你理解并利用最不知名的语言特征、技术和相关的内部信息。能够自动化大量的系统管理。)
8 - deep understanding of corner cases, esoteric features, protocols and systems including “theory of operation”. Demonstrated ability to design, deploy and own very critical or large infrastructure, build accompanying automation.(8 -深刻理解角落案例,深奥的特点,协议和系统,包括“操作理论”。演示了设计、部署和拥有非常关键或大型基础设施的能力,并建立了相应的自动化。)
9 - could have written the book about the subject area but didn’t; works with standards committees on defining new standards and methodologies.(9 -本可以写关于主题领域的书,但没有;与标准委员会一起制定新的标准和方法。)
10 - wrote the book on the subject area (there actually has to be a book). Recognized industry expert in the field, might have invented it.(10 -写在主题领域的书(实际上必须有一本书)。业内公认的业内专家,可能已经发明了它。)
Subject Areas:
TCP/IP Networking (OSI stack, DNS etc)
Unix/Linux internals
Unix/Linux Systems administration
Algorithms and Data Structures
C
C++
Python
Java
Perl
Go
Shell Scripting (sh, Bash, ksh, csh)
SQL and/or Database Admin
Scripting language of your choice (not already mentioned) _
People Management
Project Management
3.Tip:
linux内存分配
4.Share:
[互联网公司中所谓中台是什么?]
前几天看到有的地方有提到中台,就在网上搜了一些中台的描述,感觉这里面的描述的比较清楚一些,看完至少知道中台是什么,为什么会有人提出中台的概念,去解决什么问题。