剑指offer题目练习一
看见了一道二维数组找数的题,已排好序的数组(从左至右从上到下,都是由小变大的)让找数,瞬间就出思路了,并没有必要去看他的解释,两次二分就搞定了。
- #include<cstdio>
- #include<iostream>
- using namespace std;
- void sreach(int num[][], int row, int line, int goal)
- {
- int i=,j=row-,mid;
- while((j-i)>)
- {
- mid=(i+j)/;
- if(num[mid][]>goal) j=mid;
- else i=mid;
- }
- int a,b;
- a = goal>=num[j][] ? j : i;
- i=,j=line-;
- while((j-i)>)
- {
- mid=(i+j)/;
- 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[][];
- while(scanf("%d%d",&m,&n))
- {
- for(int i=;i<n;i++)
- for(int j=;j<m;j++)
- scanf("%d",&number[i][j]);
- int x;
- cin>>x;
- sreach(number, n, m, x);
- }
- return ;
- }
- /*
- 6 6
- 1 2 3 4 5 6
- 10 12 15 16 18 21
- 22 23 26 27 29 30
- 32 33 34 35 36 37
- 40 41 43 44 47 50
- 50 51 55 56 57 58
- 5 5
- 1 2 3 4 5
- 10 12 15 16 18
- 22 23 26 27 29
- 32 33 34 35 36
- 40 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=;
- 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[];
- 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);
- }
- }
剑指offer题目练习一的更多相关文章
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...
- python 修改xml文件
在百度知道里面有人问了这么一个问题: 有一个xml文件:<root>text <a/> <a/> ...这里省略n个<a/> <root>想 ...
- (转)轻松解决 MyEclipse、Eclipse 编译时提示 @Override The method of type must override a superclass method 即 @Override 标注问题
刚才在把工程从其他地方导入到自己机子的 MyEclipse 下时,出现了 The method of type must override a superclass method ,提示的是实现类必须 ...
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- Servlet是线程安全的吗?
Servlet不是线程安全的. 要解释为什么Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP请求时 ...
- 计算结构体、数组、指针的sizeof
1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...
- MYSQL导入excel
MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...
- S7-1500 读取V90/S120/S210/G120的常用驱动参数
S7-1500 读取V90/S120的常用驱动参数 此程序已更新,可以下载例子程序 https://files.cnblogs.com/files/lion-zheng/PLC_async_drive ...
- C语言函数申明关键字inline
内联inline是给编译器的优化提示,如果一个函数被编译成inline的话,那么就会把函数里面的代码直接插入到调用这个函数的地方,而不是用调用函数的形式.如果函数体代码很短的话,这样会比较有效率,因为 ...
- Vue路由讲解
1>router-link和router-view组件 2>路由配置 a.动态路由 import Home from "@/views/Home.vue"; expor ...