java版数据结构与算法—数组、替换字符串中的空格、找出旋转数组中最小值

class ArrayTest {

   private long[] a;
   private int nElems;
   public ArrayTest(int max){
      a = new long[max];
      nElems = 0;
   }

   //查找指定的项
   public boolean find(long searchKey){
      int i;
      for(i=0; i<nElems; i++){
         if(a[i] == searchKey){
            break;
         }
      }

      if(i == nElems){
         System.out.println("Can't found:" + searchKey);
         return false;
      }else {
         System.out.println("find it:" + searchKey);
         return true;
      }
   }

   //插入
   public void insert(long value){
      a[nElems] = value;
      nElems ++;
   }

   //删除
   public boolean delete(long value){
      int i;
      for(i=0; i<nElems; i++){
         if(a[i] == value){
            break;
         }
      }

      if(i == nElems){
         return false;
      }else {
         for(int k=i; k<nElems; k++){
            a[k] = a[k+1];
         }
         nElems --;
         return true;
      }
   }

   //查找所有元素
   public void findAll(){
      for(int i=0; i<nElems; i++){
         System.out.print(a[i] + " ");
      }
      System.out.println();
   }

   public static void main(String[] args){
      ArrayTest a = new ArrayTest(100);
      a.insert(11);
      a.insert(18);
      a.insert(16);
      a.insert(1);
      a.insert(88);
      a.insert(66);
      a.insert(25);
      a.insert(0);
      a.insert(35);
      a.insert(77);

      a.findAll();

      a.find(88);

      a.delete(66);
      a.delete(1);

      a.findAll();
   }
}


在这里插入图片描述

package com.zoujc.tes3;

/**
 * 将空格替换成“abc”
 * @author zoujc
 * @date 2018/11/27
 */
public class ReplaceBlank {
	public static String replaceBlankStr(String str){
		if(str.length() == 0 || str == null){
			return null;
		}
		//计数空白字符的个数
		int count = 0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i) == ' '){
				count ++;
			}
		}
		//计算原来字符串的长度
		int oldLen = str.length();
		//扩展数组长度
		int newLen = oldLen + count * 2;
		char[] newChar = new char[newLen];
		System.arraycopy(str.toCharArray(),0,newChar,0,str.length());
		//定义两个游标,i指向newChar数组中的当前值,j指向newChar初始化的值,
		//然后从后往前初始化newChar数组,直到i指向0或者j追上i
		int i = oldLen - 1, j = newLen - 1;
		while (i >= 0 && i < j){
			if(newChar[i] == ' '){
				newChar[j --] = 'c';
				newChar[j --] = 'b';
				newChar[j --] = 'a';
			}else {
				newChar[j --] = newChar[i];
			}
			i --;
		}
		String newStr = new String(newChar);
		return newStr;
	}
	public static void main(String[] args){
	 	String str = "1 2 3 5  8 ";
	 	str = replaceBlankStr(str);
	 	System.out.println(str);
	}
}


在这里插入图片描述

package com.zoujc.test2;

/**
 *  把一个数组最开始的若干个元素搬到数组的末尾, 我们称之数组的旋转。
 *  输入一个递增排序的数组的一个旋转, 输出旋转数组的最小元素。
 *  例如数组{3,4, 5, 1, 2 }为{ l1,2,3, 4,5}的一个旋转,该数组的最小值为1
 * @author zoujc
 * @date 2018/11/27
 */
public class TurnArray {
	public static int getMin(int[] arr){
		int index1 = 0;
		int index2 = arr.length - 1;
		int mid = 0;
		// 确保index1在前一个排好序的部分,index2在排好序的后一个部分
		while(arr[index1] >= arr[index2]){
			if(index2 - index1 == 1){
				mid = index2;
				break;
			}
			//取中间值
			mid = (index1 + index2) / 2;
			// 如果下标为index1和index2以及mid指向的三个数字都相等,则只能顺序查找
			if(arr[index1] == arr[index2] && arr[index1] == arr[mid]){
				return minInOrder(arr,index1,index2);
			}
			// 如果中间位置对应的值在前一个排好序的部分,将index1设置为新的处理位置
			if(arr[mid] >= arr[index1]){
				index1 = mid;
				// 如果中间位置对应的值在后一个排好序的部分,将index2设置为新的处理位置
			}else if(arr[mid] <= arr[index2]){
				index2 = mid;
			}
		}
		//返回最终结果
		return arr[mid];
	}

	//找数组中的最小值
	public static int minInOrder(int[] arr,int index1,int index2){
		int min = arr[index1];
		for(int i=index1 + 1;i<= index2;i++){
			if(min > arr[i]){
				min = arr[i];
			}
		}
		return min;
	}

	public static void main(String[] args) {
		// 典型输入,单调升序的数组的一个旋转
		int[] array1 = {3, 4, 5, 1, 2};
		System.out.println(getMin(array1));

		// 有重复数字,并且重复的数字刚好的最小的数字
		int[] array2 = {3, 4, 5, 1, 1, 2};
		System.out.println(getMin(array2));

		// 有重复数字,但重复的数字不是第一个数字和最后一个数字
		int[] array3 = {3, 4, 5, 1, 2, 2};
		System.out.println(getMin(array3));

		// 有重复的数字,并且重复的数字刚好是第一个数字和最后一个数字
		int[] array4 = {1, 0, 1, 1, 1};
		System.out.println(getMin(array4));

		// 单调升序数组,旋转0个元素,也就是单调升序数组本身
		int[] array5 = {1, 2, 3, 4, 5};
		System.out.println(getMin(array5));

		// 数组中只有一个数字
		int[] array6 = {2};
		System.out.println(getMin(array6));

		// 数组中数字都相同
		int[] array7 = {1, 1, 1, 1, 1, 1, 1};
		System.out.println(getMin(array7));
		System.out.println(getMin(array6));

		// 输入NULL
//		System.out.println(getMin(null)); //java.lang.NullPointerException

	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值