博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ctci(1)
阅读量:6918 次
发布时间:2019-06-27

本文共 10333 字,大约阅读时间需要 34 分钟。

////  main.cpp//  proj1////  Created by  Yuxin Kang on 8/24/14.//  Copyright (c) 2014  Yuxin Kang. All rights reserved.//#include 
#include
#include
#include
using namespace std; //for cin and cout //==================cin & cout =================== /* int main(int argc, const char * argv[]) { int x; // insert code here... std::cout << "Hello, World!!\n"; std::cout << "Please enter x\n"; std::cin >> x; if (cin.fail()){ cout << "Bad input\n"; return 0; } std::cout << "you have entered the number of "<
<<"\n"; return 0; } */ //==================================================== ///==============memset================================ // int main(int argc, const char * argv[])// {// char str[] = "almost every programmer should know memset";// memset (str,'-',6);// puts (str);// return 0;// } //===================================================== //==============ASCII_cout============================= // int main(int argc, const char * argv[]) // {// string s="09aZ i am kang ";// for(int i=0;i< 5; ++i){// int v = (int)s[i];// cout << v <<"\n" ;// }// // for (int i=1 ;i< 128;i++)// {// cout<
<<"=="<<(char)i<<"\n";// }// } //=================================================== //=================================================== //1. 1 Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures?// int main(int argc, const char * argv[])// {// string s1= "czc";// int check = 0;// int len = s1.length();// for(int i=0;i
<<"\n"<
<<"\n(2)"<
top// matrix[first][j]=matrix[last-offset][first];// //bottom->left// matrix[last-offset][first]=matrix[last][last-offset];// //right->bottem// matrix[last][last-offset]=matrix[j][last];// //top->right// matrix[j][last] = top;// // }// // }//}////void transpose(int a[][4], int n){// for(int i=0; i
>m>>n;// int **a;// a=new int*[m];// for(int i=0;i
>a[i][j];// for(int i=0;i
data=a[i];// if(i==0)// {// head=p=nd; //p指向第一个节点 相当于创建了节点nd,装入p节点中// continue;// }// p->next=nd;// p=nd;// }// return head;//}////void removeduplicate(node *head)//{// if(head==NULL) return ;// node *p=head, *q=head->next;// hash1[head->data]=true; //因为数组坐标是唯一的 将head的值去占位,表示已经存在// while(q)// {// if(hash1[q->data]) //表示下一个节点(p)在hash数组里面寻找// {// //存在// node *t=q; //待删除节点// p->next=q->next;// q=p->next;// delete t;// }// else// {// //not exist ,move// hash1[q->data]=true;// p=q;// q=q->next;// // }// }//}//////void removeduplicate2(node *head)//{// if(head==NULL) return;// node *p,*q,*c=head; //定义三个指针,c指向当前要检测的节点,p和q两个负责分别往后去检测有没有和c相同的节点,然后进行删除// while(c)// {// p=c;q=c->next;// int curdata = c->data;// while(q)// {// if(curdata==q->data)// {// node *t =q; //准备删除q// p->next=q->next;// q=p->next;// }// else// {// p=q;// q=q->next;// }// }// c=c->next;// }// //}////void print(node* head)//{// while(head)// {// cout<
data;// head=head->next;// }//}////int main()//{// int n=6;// int a[]={3,2,2,1,4,1}; //里面的值去对应hash1数组的编号// memset(hash1,false,sizeof(hash1));// node *head=init(a, n);// removeduplicate(head);// print(head);// return 0;//}////typedef struct node//{// node* next;// int data;//}node;////node* init(int a[], int n)//{// node* head,*p;// for(int i=0;i
data=a[i];// if(i==0)// {// head=p=nd;// continue;// }// p->next=nd;// p=nd;// }// return head;//}////node* nthTolast(node* head,int n)//{// if(head==NULL||n<1)// return NULL;// node* p1=head;// node* p2=head;// for(int i=0;i
next;// }// // while(p2->next!=NULL)// {// p1=p1->next;// p2=p2->next;// }// return p1;//}//int main()//{// int n=10;// int a[]={9,2,3,5,6,7,8,9,1,0};// node* head = init(a, n);// node *p=nthTolast(head, 5);// if(p) cout<
data<
data=a[i];// if(i==0)// {// head=p=nd;// continue;// }// p->next=nd;// p=nd;// }// return head;//}////bool remove(node* p)//{// if(p==NULL||p->next==NULL) return false;// //删除节点// node* t = p->next;// p->next=t->next;// delete t;// // return true;//}////void print (node* head)//{// while(head)// {// cout<
data;// head=head->next;// }//}//int main()//{// int a[]={9,2,3,5,6,7,8,9,1,0};// //node* head=init(a,10);//第二个参数必须是变量;error// int n=10;// node*p,* head;// head=p=init(a,n); //初始化数组到list中// cout<<"please enter the number of nd in the list you want to delete";// int m; //要删除第m个// cin>>m;// for (int i=1;i
next; //p指针向后移动// if(remove(p)&&m!=1)// print(head);// else// cout<<"failure";// //}////2 4//You have two numbers represented by a linked list, where each node contains a sin- gle digit The digits are stored in reverse order, such that the 1’s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list//typedef struct node//{// node* next;// int data;//}node;////node* init(int a[] ,int n)//{//// node* head,*p;// for(int i=0;i
data=a[i];// if(i==0)// {// head=p=nd;// continue;// }// p->next=nd;// p=nd;// }// return head;//}////node* addlink(node*p,node*q)//{// if(p==NULL) return q;// if(q==NULL) return p;// node* head =NULL;//计算后结果保存的node的头指针 的存在意义是为了以后为了返回整个list// node* cur = NULL;//current point 的存在意义是为了链接新生成的节点// int c=0; //前面计算的进位// while(p&&q)// {// node* r=new node();// int t=(q->data)+(p->data);// r->data=(c+t)%10;// c=t/10;// p=p->next;// q=q->next;// if(cur)// {// cur->next=r;// cur=r;// }// else// {// head=cur=r;// }// }// // while(p)// {// int t= p->data+c;// node* r = new node();// r->data=t%10;// c=t/10;// cur->next=r;// cur=r;// p=p->next;// }// while(q)// {// int t= q->data+c;// node* r = new node();// r->data=t%10;// c=t/10;// cur->next=r;// cur=r;// q=q->next;// }// if(c>0)// {// node* r=new node();// r->data=c;// cur->next=r;// }// // return head;//}////void print(node* head)//{// while(head)// {// cout<
data;// head=head->next;// }// cout<<"\n";//}//int main()//{// int a[]={1,2,9,9,3};// int b[]={9,9,2};// int m=sizeof(a)/4;// int n=sizeof(b)/4;// node *p1,*p2,*res;// p1=init(a,m);// p2=init(b,n);// print(p1);// print(p2);// res=addlink(p1,p2);// print(res);//}//2 5//Given a circular linked list, implement an algorithm which returns node at the begin- ning of the loop//typedef struct node//{// node* next;// int data;//}node;////node* init(int a[],int m,int n)//{// node* head,*p; //head负责回传头指针 p负责链接每个节点// node* q;//负责指向环链接的开始// for(int i=0;i
data=a[i];// if(i==0)// {// head=p=nd;// continue;// }// if(i==m)// {// q=nd; //q负责指向环链接的开始// }// p->next=nd;// p=nd;// }// p->next=q;// return head;//}//////void loopstart(node* head)//{// node* fast,*slow; //// fast=slow=head;// do// {// if(fast->next->next==NULL||slow->next==NULL)// {// break;// // return 0;// }// fast=fast->next->next;// slow=slow->next;// }while(slow!=fast);// slow=head;//slow回到起点// while(slow!=fast)// {// fast=fast->next;// slow=slow->next;// }// // return fast;// cout<<"the starting point of the cycle is "<
data<
totalSize=totalSize; //----->不理解// cur=0;//当前第几个数组// }// ~stack()// {// delete []buf;// }// // void push(int stackNum, int val) // 当前组,当前数字// {// buf[cur].val=val;// buf[cur].preIdx=ptop[stackNum];// ptop[stackNum]=cur; //保存最高点的-位置编号// ++cur;// }// // void pop(int stackNum)// {// ptop[stackNum]=buf[ptop[stackNum]].preIdx; //将指向顶部的编号指向前一个编号// }// // int top(int stackNum)// {// return buf[ptop[stackNum]].val;// }//private: //stack的每个属性// node *buf;// int ptop[3]; //分三段 标记顶的值// int totalSize;// int cur;//};//////int main()//{// stack mystack;// for(int i=0;i<10;i++)// mystack.push(0,i);// for(int i=10;i<20;i++)// mystack.push(1,i);// for(int i=100;i<200;i++)// mystack.push(2, i);// // for(int i=0;i<3;i++)// {// //cout<
//struct op{// int begin, end;// char start,buff,dest;// op(){} //op(){}// op(int pbegin,int pend, int pstart,int pbuff,int pdest):begin(pbegin), end(pend), start(pstart), buff(pbuff),dest(pdest){} //for struct init ,存放在stack里面//};//void haoni(int n, char start, char buff, char dest)//{// stack
st; //生成存放op的stack// op temp;// st.push(op(1,n,start,buff,dest));// while (!st.empty())// {// temp=st.top();//获取stack中当前的op,开始就是main中输入的(A,B,C),输入进temp,然后循环获取top输入进temp// st.pop();//指针向下移动,又从-1开始 ,前面的相当于不在了,只是输给temp// // if(temp.begin!=temp.end)// {// //初始化这个op再压入stack// st.push(op(temp.begin,temp.end-1,temp.buff,temp.start,temp.dest));//递归完成从B柱->到c柱 (3步骤)// st.push(op(temp.end,temp.end,temp.start,temp.buff,temp.dest));//Move disk 3 from A to C(2步骤)// st.push(op(temp.begin,temp.end-1,temp.start,temp.dest,temp.buff));//递归完成从A柱->到B柱 (1步骤)// }else// {// cout<<"Move disk "<
<<" from "<
<<" to "<
<
//#include
//#include
//template
//class MyStack//{//public:// void push(T val)// {// qin.push(val);// }// void pop()// {// if(!qin.empty())// move(qin, qout);// else// move(qout,qin);// }// T top()// {// if(qin.empty())// {// return qout.back();// }// else return qin.back();// }// // void move(queue
&src, queue
&dest) //思想是不管从谁到谁,我只管我这个函数开始到目标,和输入无关// {// while(!src.empty())// {// T val= src.front();// src.pop();// if(src.empty()) break;// dest.push(val);// // }// }// int size()// {// return qin.size()+qout.size();// }// //private:// queue
qin, qout;//};////////template
//class MyQueue//{//public:// MyQueue()// {// }// ~MyQueue()// {// }// void push(T val)// {// sin.push(val);// }// void pop()// {// move(sin,sout);// sout.pop();// // }// T front()// {// move(sin,sout);// return sout.top();// }// T back() //**和上面相反// {// move(sout,sin);// return sin.top();// }// int size()// {// return sin.size()+sout.size();// }// bool empty()// {// return sin.empty()&&sout.empty();// }// void move(stack
&src,stack
&dest)// {// if(dest.empty())// {// while(!src.empty())// {// dest.push(src.top());// src.pop();// }// }// }// //private:// stack
sin, sout;//};////int main()//{// MyStack
st;// for(int i=0;i<10;i++)// {// st.push(i);// }// cout<<"stack top: "<
<
q;// for(int i=0;i<10;i++)// {// q.push(i);// }// // cout<<"front: "<
<<" back: "<
<
//stack
sSort(stack
st)//{// stack
newSt;// while (!st.empty()) {// int max = st.top();// st.pop();// while (!newSt.empty()&&newSt.top()>max) {// st.push(newSt.top());// newSt.pop();// }// newSt.push(max);// }// return newSt;//}////int main()//{// stack
st;// for(int i=0;i<10;i++)// {// st.push(rand()%10);// }// stack
s = sSort(st);// while(!s.empty())// {// cout<

  

转载于:https://www.cnblogs.com/kyxyes/p/3962348.html

你可能感兴趣的文章
我的友情链接
查看>>
CAD设计你需要以下几款开源工具
查看>>
如何提高百度文库的通过率
查看>>
周珍:浮躁的互联网,造就了坚韧的SEO
查看>>
mysql在linux上导数据库
查看>>
排错-升级Exchange 2013 CU22后程序名称显示异常
查看>>
数据同步备份简单脚本
查看>>
初入IT
查看>>
Webpack4干货分享(二),使用loader处理scss,图片以及转换JS
查看>>
LAMP自动安装脚本第三版
查看>>
概率论与数理统计(一)概率的定义与性质
查看>>
springboot导出excel
查看>>
Cloudera-scm-server启动报org.hi bernate.exception.GenericJDBCException: Could not open connection
查看>>
SylixOS iMX6平台I2C总线驱动
查看>>
聊一聊 各种验证邮件地址的正则表达式
查看>>
Python代码规范和命名规范
查看>>
npm install:ChromeDriver installation failed Error with http(s) request: Error: read ETIMEDOUT
查看>>
AIDE使用
查看>>
CentOS7查看CPU个数
查看>>
显微镜下的webpack4:灵魂tapable,终于搞懂钩子系列!
查看>>