剑指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个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- Android NDK 入门与实践
NDK 是什么 NDK 全称 Native Development Kit,可以让您在 Android 应用中调用 C 或 C++ 代码的工具. NDK 好处 1.NDK 可以生成 .so 文件, 方 ...
- 【起航计划 017】2015 起航计划 Android APIDemo的魔鬼步伐 16 App->Alarm->Alarm Controller Alarm事件 PendingIntent Schedule AlarmManager
Alarm Controller演示如何在Android应用中使用Alarm事件,其功能和java.util.Timer ,TimerTask类似.但Alarm可以即使当前应用退出后也可以做到Sche ...
- shell脚本学习(3)文件判断
shell常用的文件判断运算符如下: -e 文件是否存在 -f 文件是否是普通文件(不是目录.设备文件.链接文件) -s 表示文件大小不为0 -d 表示文件是否是目录 -b 表示是块设备(光驱.软 ...
- So you want to write a desktop app in Python
So you want to write a desktop app in Python Thomas Kluyver 2014-06-16 23:55 51 Comments Source This ...
- May 03rd 2017 Week 18th Wednesday
Truth needs no colour; beauty, no pencil. 真理不需要色彩,美丽不需要涂饰. There is no absoulte truth and everlastin ...
- windows 右健添加cmd快捷通道
windows 右健添加cmd快捷 - Windows - geektown极客堂 - Powered by Discuz!. 把横线下面的文本copy保存到一个注册表文件中,比如cmd.reg,然后 ...
- Last_Errno: 1396
Last_Errno: 1396 Last_Error: Error 'Operation CREATE USER failed for 'usera63'@'%'' on query. Defa ...
- Android(java)学习笔记59:类继承的 注意事项
1. 类继承的注意事项: /* 继承的注意事项: A:子类只能继承父类所有非私有的成员(成员方法和成员变量) B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法. ...
- HDU(4394),数论上的BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394 思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继 ...
- cudaMemcpy2D介绍
cudaMemcpy2D( d_A, // 目的指针 d_pitch, // 目的pitch bmp1, // 源指针 sizeof(int)*2, // 源数据pitch sizeof(int)*2 ...