查看: 1223| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] 关于eclipse我有一个问题

🔗
MTC | 只看该作者 |倒序浏览
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. /*这样是可以debug的*/
  2. package q11_5;

  3. import java.util.Arrays;

  4. public class Q11_5{

  5. public static int searchR(String[] strings,
  6.                 String str,int first,int last){
  7.         if(first>last){
  8.                
  9.                 return -1;
  10.         }

  11. int mid=(first+last)/2;
  12. if(strings[mid].isEmpty()){

  13. int left=mid-1;
  14. int right=mid+1;
  15. while(true){
  16. if(left<first&&right>last){//through so many manipulations of BS,only '' left in this array

  17.         return -1;
  18. }else if(right<=last&&!strings[right].isEmpty()){

  19.     mid=right;
  20.     break;
  21. }else if(left>=first&&!strings[left].isEmpty()){

  22. mid=left;
  23. break;


  24. }
  25. left--;
  26. right++;
  27. }
  28. }

  29. /*recursive*/

  30. if(str.equals(strings[mid])){
  31.         return mid;
  32. }else if(strings[mid].compareTo(str)<0){//search thr right part

  33. return searchR(strings,str,mid+1,last);

  34. }else{
  35.         return searchR(strings,str,first,mid-1);
  36. }
  37. }


  38. public static int search(String[] strings,String str){
  39. if(strings==null||str==null||str==""){
  40. return -1;

  41. }

  42. return searchR(strings,str,0,strings.length-1);
  43. }


  44.         public static void main(String[] args) {
  45.                 String[] strings={"Tong", "apple",
  46.                                 "banana", "chaing", "click", "tom"};
  47.                 System.out.println(search(strings,"tom"));
  48.         }
  49. }

  50. /main函数改为int就不行了*/
  51. package q11_5;

  52. import java.util.Arrays;

  53. public class Q11_5{

  54. public static int searchR(String[] strings,
  55.         String str,int first,int last){
  56.     if(first>last){
  57.         
  58.         return -1;
  59.     }

  60. int mid=(first+last)/2;
  61. if(strings[mid].isEmpty()){

  62. int left=mid-1;
  63. int right=mid+1;
  64. while(true){
  65. if(left<first&&right>last){//through so many manipulations of BS,only '' left in this array

  66.     return -1;
  67. }else if(right<=last&&!strings[right].isEmpty()){

  68.     mid=right;
  69.     break;
  70. }else if(left>=first&&!strings[left].isEmpty()){

  71. mid=left;
  72. break;


  73. }
  74. left--;
  75. right++;
  76. }
  77. }

  78. /*recursive*/

  79. if(str.equals(strings[mid])){
  80.     return mid;
  81. }else if(strings[mid].compareTo(str)<0){//search thr right part

  82. return searchR(strings,str,mid+1,last);

  83. }else{
  84.     return searchR(strings,str,first,mid-1);
  85. }
  86. }


  87. public static int search(String[] strings,String str){
  88. if(strings==null||str==null||str==""){
  89. return -1;

  90. }

  91. return searchR(strings,str,0,strings.length-1);
  92. }


  93.   public static int main(String[] args) {
  94.         String[] strings={"Tong", "apple",
  95.                 "banana", "chaing", "click", "tom"};
  96.         return search(strings,"chiang");
  97.     }
  98. }

复制代码
会发现没有java application的选项


上一篇:字符串数组默认按照什么顺序排序?
下一篇:LinkedList&lt;AnyType&gt; implements Iterable&lt;AnyType&gt; 求解释
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表