看见了一道二维数组找数的题,已排好序的数组(从左至右从上到下,都是由小变大的)让找数,瞬间就出思路了,并没有必要去看他的解释,两次二分就搞定了。

#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题目练习一的更多相关文章

  1. 代码题 — 剑指offer题目、总结

    剑指offer题目总结:  https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...

  2. 剑指offer题目系列三(链表相关题目)

    本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...

  3. 再来五道剑指offer题目

    再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...

  4. 剑指 Offer 题目汇总索引

    剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格              ...

  5. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

  6. 剑指offer题目java实现

    Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...

  7. 剑指offer题目系列二

    本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...

  8. 剑指offer题目系列一

    本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...

  9. 【剑指Offer】剑指offer题目汇总

      本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...

  10. 剑指offer题目解答合集(C++版)

    数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...

随机推荐

  1. nginx的常用命令

    一.nginx的解压安装 #tar xzvf nginx-1.6.0.tar.gz #cd nginx-1.6.0 #./configure    --prefix=/home/weixin/loca ...

  2. April 11 2017 Week 15 Tuesday

    Love is hard to get into, but harder to get out of. 相爱不易,相忘更难. The past are hurt, but I think we can ...

  3. windows如何关闭指定端口

    关闭windows中被占用的端口,比如我们常见的8080端口被占用了 1.查找端口的PID netstat -aon|findstr "8080" 如图 PID为3888 2.关闭 ...

  4. vue.js 错误提示bash: vue: command not found

    在使用 vue init webpack vue-demo 进行demo的下载时,提示vue: command not found,原因是环境变量没有进行配置,所以会出现这个问题,解决办法 找到你安装 ...

  5. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

    cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...

  6. 起一个node服务

    使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...

  7. 【转】startActivityForResult和setResult详解

    startActivityForResult与startActivity的不同之处在于:1.startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startAct ...

  8. 第42章 电源管理—实现低功耗—零死角玩转STM32-F429系列

    第42章     电源管理—实现低功耗 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  9. css3 子元素的的应用 注意点

    已经第二次犯错误,不允许有下次 <ul class="ul"> <li> <a>哈哈</a> </li> <li& ...

  10. 前端css优先级以及继承

    1.css优先级以及继承 css具有两大特性:继承性和层叠性 继承性 继承:给父级设置一些属性,子级继承了父级的该属性,这就是我们的css中的继承. 有一些属性是可以继承下来 : color . fo ...