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

  1. #include<cstdio>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5. void sreach(int num[][], int row, int line, int goal)
  6. {
  7. int i=,j=row-,mid;
  8. while((j-i)>)
  9. {
  10. mid=(i+j)/;
  11. if(num[mid][]>goal) j=mid;
  12. else i=mid;
  13. }
  14. int a,b;
  15. a = goal>=num[j][] ? j : i;
  16. i=,j=line-;
  17. while((j-i)>)
  18. {
  19. mid=(i+j)/;
  20. if(num[a][mid]>goal) j=mid;
  21. else i=mid;
  22. }
  1.  

if(num[a][b]!=goal){
cout<<"not found!"<<endl;
}

  1.  

else cout<<a<<","<<b<<endl;

  1. }
  2. int main()
  3. {
  4. int n,m;
  5. int number[][];
  6. while(scanf("%d%d",&m,&n))
  7. {
  8. for(int i=;i<n;i++)
  9. for(int j=;j<m;j++)
  10. scanf("%d",&number[i][j]);
  11. int x;
  12. cin>>x;
  13. sreach(number, n, m, x);
  14. }
  15. return ;
  16. }
  17. /*
  18. 6 6
  19. 1 2 3 4 5 6
  20. 10 12 15 16 18 21
  21. 22 23 26 27 29 30
  22. 32 33 34 35 36 37
  23. 40 41 43 44 47 50
  24. 50 51 55 56 57 58
  25. 5 5
  26. 1 2 3 4 5
  27. 10 12 15 16 18
  28. 22 23 26 27 29
  29. 32 33 34 35 36
  30. 40 41 43 44 47
  31. */

还有一道插入字符串,把空格都换成%D%,直接链表搞定,不让用链表就用JAVA的linkedlist写。一个函数就够了。

  1. #include<cstdio>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. char data;
  9. node *next;
  10. }*root; // 指针没有盘空
  11. void setuplist(char s[])
  12. {
  13. node *temp,*n;
  14. n=root;
  15. int i=;
  16. while(s[i]!='\0')
  17. {
  18. temp = new node;
  19. temp->next=NULL;
  20. temp->data=s[i];
  21. n->next=temp;
  22. n=temp;
  23. i++;
  24. }
  25. }
  26. void insertwords()
  27. {
  28. node *n;
  29. node *temp1,*temp2;
  30. n=root->next;
  31. while(n->next)
  32. {
  33. if(n->data==' ')
  34. {
  35. n->data='%';
  36. temp1 = new node;
  37. temp1->data = 'd';
  38. temp2 = new node;
  39. temp2->data = '%';
  40. temp1->next=temp2;
  41. temp2->next=n->next;
  42. n->next=temp1;
  43. }
  44. else n=n->next;
  45. }
  46. }
  47. void del(node *n)
  48. {
  49. if(n->next) del(n->next);
  50. delete n;
  51. }
  52. int main()
  53. {
  54. char s[];
  55. while(gets(s))
  56. {
  57. root = new node;
  58. root->next = NULL;
  59. setuplist(s);
  60. insertwords();
  61. node *t=root->next;
  62. while(t->next)
  63. {
  64. cout<<t->data;
  65. t=t->next;
  66. }
  67. cout<<endl;
  68. del(root);
  69. }
  70. }

剑指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. 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser

    Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...

  2. python 修改xml文件

    在百度知道里面有人问了这么一个问题: 有一个xml文件:<root>text <a/> <a/> ...这里省略n个<a/> <root>想 ...

  3. (转)轻松解决 MyEclipse、Eclipse 编译时提示 @Override The method of type must override a superclass method 即 @Override 标注问题

    刚才在把工程从其他地方导入到自己机子的 MyEclipse 下时,出现了 The method of type must override a superclass method ,提示的是实现类必须 ...

  4. Struts_OGNL(Object Graph Navigation Language) 对象图导航语言

    1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...

  5. Servlet是线程安全的吗?

    Servlet不是线程安全的. 要解释为什么Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP请求时 ...

  6. 计算结构体、数组、指针的sizeof

    1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...

  7. MYSQL导入excel

    MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...

  8. S7-1500 读取V90/S120/S210/G120的常用驱动参数

    S7-1500 读取V90/S120的常用驱动参数 此程序已更新,可以下载例子程序 https://files.cnblogs.com/files/lion-zheng/PLC_async_drive ...

  9. C语言函数申明关键字inline

    内联inline是给编译器的优化提示,如果一个函数被编译成inline的话,那么就会把函数里面的代码直接插入到调用这个函数的地方,而不是用调用函数的形式.如果函数体代码很短的话,这样会比较有效率,因为 ...

  10. Vue路由讲解

    1>router-link和router-view组件 2>路由配置 a.动态路由 import Home from "@/views/Home.vue"; expor ...