剑指offer题目练习一
来源:互联网 发布:手机oa软件 编辑:程序博客网 时间:2024/06/01 08:52
看见了一道二维数组找数的题,已排好序的数组(从左至右从上到下,都是由小变大的)让找数,瞬间就出思路了,并没有必要去看他的解释,两次二分就搞定了。
#include<cstdio>#include<iostream>using namespace std;void sreach(int num[][100], int row, int line, int goal){ int i=0,j=row-1,mid; while((j-i)>1) { mid=(i+j)/2; if(num[mid][0]>goal) j=mid; else i=mid; } int a,b; a = goal>=num[j][0] ? j : i; i=0,j=line-1; while((j-i)>1) { mid=(i+j)/2; if(num[a][mid]>goal) j=mid; else i=mid; }
if(num[a][b]!=goal){
cout<<"not found!"<<endl;
}
else cout<<a<<","<<b<<endl;
}int main(){ int n,m; int number[100][100]; while(scanf("%d%d",&m,&n)) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&number[i][j]); int x; cin>>x; sreach(number, n, m, x); } return 0;}/*6 61 2 3 4 5 610 12 15 16 18 2122 23 26 27 29 3032 33 34 35 36 3740 41 43 44 47 5050 51 55 56 57 585 51 2 3 4 510 12 15 16 1822 23 26 27 2932 33 34 35 3640 41 43 44 47*/
还有一道插入字符串,把空格都换成%D%,直接链表搞定,不让用链表就用JAVA的linkedlist写。一个函数就够了。
#include<cstdio>#include<iostream>using namespace std;struct node{ char data; node *next;}*root; // 指针没有盘空void setuplist(char s[]){ node *temp,*n; n=root; int i=0; while(s[i]!='\0') { temp = new node; temp->next=NULL; temp->data=s[i]; n->next=temp; n=temp; i++; }}void insertwords(){ node *n; node *temp1,*temp2; n=root->next; while(n->next) { if(n->data==' ') { n->data='%'; temp1 = new node; temp1->data = 'd'; temp2 = new node; temp2->data = '%'; temp1->next=temp2; temp2->next=n->next; n->next=temp1; } else n=n->next; }}void del(node *n){ if(n->next) del(n->next); delete n;}int main(){ char s[100]; while(gets(s)) { root = new node; root->next = NULL; setuplist(s); insertwords(); node *t=root->next; while(t->next) { cout<<t->data; t=t->next; } cout<<endl; del(root); }}
0 0
- 剑指offer题目练习一
- 剑指offer题目汇总(一)
- 剑指offer练习(一)
- 剑指offer练习Python(一)
- 剑指offer 练习一(Java版)
- 剑指offer之Python练习一
- 牛客网做题总结:剑指offer中题目,java版一
- 剑指Offer题目JAVA版思路与代码(一)
- 剑指offer题目集
- 剑指offer题目记录
- 剑指offer题目概括
- 剑指offer题目
- 【剑指offer】题目4
- 【剑指offer】题目10
- 剑指offer题目分类
- 剑指offer题目1
- 剑指offer 练习
- 《剑指offer》练习
- 斯坦福大学机器学习公开课, 梯度下降算法,随机梯度下降算法,最小二乘拟合法总结以及证明
- 平衡二叉树+树的旋转
- 程序的性能优化之代码上的细节优化
- 一个绝对注意不到的小细节(深入理解计算机系统第五章5.5及5.6)
- 使用C++封装互斥锁的实践
- 剑指offer题目练习一
- 代码细节的终极优化之循环展开、多路并行
- JSON和XML
- 二叉树习题之重建二叉树
- 指针与++
- 简易俄罗斯方块
- Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效
- android – 多屏幕适配相关
- 最短路径算法总结(Floyd,bellmen-ford,dijkstra,Spfa)