数组中重复的数字、二维数组中的查找、 替换空格(字符串)

这篇博客涵盖了数组中重复数字的查找方法,通过for循环和while循环实现;二维数组中的查找算法,从特定位置开始遍历;以及字符串替换空格的解决方案,利用StringBuffer类进行高效操作。
摘要由CSDN通过智能技术生成

一、数组中的重复数字
1.题目描述
在这里插入图片描述
2.解题思路:利用for循环嵌套while循环来遍历,for循环用来遍历数组,while用来遍历数组中的某一个元素,使得nums[i] != i,若在while中瞒足nums[i] == nums[nums[i]],则说明找到了该重复的数字,返回true。
3.代码

package sword;

public class Repeat {
	public  boolean duplicate(int[] nums, int length,int[] duplication) {
		if(nums == null || length <= 0) {
			return false;
		}
		for(int i = 0; i < length; i++) {
			while(nums[i] != i) {
				if(nums[i] == nums[nums[i]]) {
					duplication[0] = nums[i];
					return true;
				}
				swap(nums,i,nums[i]);
			}
		}
		return false;
	}
	private static void swap(int[] nums, int i, int j) {
		int t = nums[i];
		nums[i] = nums[j];
		nums[j] = t;
	}
}

二、二维数组中的查找(来自:剑指面试题3)
1.题目描述
在这里插入图片描述
2.解题思路:首先,从15(即array[0][4])开始遍历,若target小于15,则遍历array[0][4-1],若target大于15,则遍历array[0+1][4],由此遍历下去,直到array[i][j],中的i或者j小于0结束。解题思路图解如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3.代码

package sword;

public class search2array {
	public boolean Find(int target, int [][]array) {
        int rows = array.length;
        int cols = array[0].length;
		int i = 0;
		int j = cols - 1;
		while(i <= rows - 1 && j>=0) {
			if(target < array[i][j]) {
				j--;
			}
			else if(target > array[i][j]) {
				i++;
			}
			else {
				System.out.println(target);
				return true;
			}
		}
		
		return false;
	}
}

4.拓展:另外,与本题中选取右上角参照数类似,也可以选取左下角的数作为参照,但是不能选取左上角或者右下角的数作为参照,因为那将不能使用判断排除的方法。
三、替换空格(字符串)
1.题目描述
在这里插入图片描述
2.解题思路:使用StringBuffer类,在字符串后面加两个空格(根据要插入字符的个数定),首先,让P1指向B,P2指向最后一个空格,将最后一个空格的替换成B,,再执行P1–和P2–,依次将%20加入到字符串中。

StringBuffer类:与String类相似,不同的是StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。具有的方法如下:
与String类相同
  int capacity()           返回当前容量。
  char charAt(int index)      返回此序列中指定索引处的 char 值。
  void ensureCapacity(int minimumCapacity)   确保容量至少等于指定的最小值。
  void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)  将字符从此序列复制到目标字符数组 dst。
  int indexOf(String str)    返回第一次出现的指定子字符串在该字符串中的索引,即从头开始搜索,返回值为-1则代表没有该字符串。
  int indexOf(String str, int fromIndex)  从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引,即从指定索引位置开始搜索。
  int lastIndexOf(String str)     返回最右边出现的指定子字符串在此字符串中的索引,即从尾开始搜索。
  int lastIndexOf(String str, int fromIndex)   返回 String 对象中子字符串最后出现的位置,即从指定位置,从后往前搜索。
  int length()           返回长度(字符数)。
  void setCharAt(int index, char ch)   将给定索引处的字符设置为 ch。
  void setLength(int newLength)     设置字符序列的长度。
  CharSequence subSequence(int start, int end)   返回一个新的字符序列,该字符序列是此序列的子序列。
  String substring(int start)     返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
  String substring(int start, int end)  返回一个新的 String,它包含此序列当前所包含的字符子序列。
  String toString()        返回此序列中数据的字符串表示形式。
与String类不同
  public StringBuffer append(String s)  将指定的字符串追加到此字符序列。
  public StringBuffer reverse()      将此字符序列用其反转形式取代。
  public delete(int start, int end)    移除此序列的子字符串中的字符。
  public insert(int offset, int i)      将 int 参数的字符串表示形式插入此序列中。
  replace(int start, int end, String str)   使用给定 String 中的字符替换此序列的子字符串中的字符。
3.代码

package sword;

public class ReplaceSpace {
	public String replaceSpace(StringBuffer str) {
	    int P1 = str.length() - 1;
	    for (int i = 0; i <= P1; i++)
	        if (str.charAt(i) == ' ')
	            str.append("  ");

	    int P2 = str.length() - 1;
	    while (P1 >= 0 && P2 > P1) {
	        char c = str.charAt(P1--);
	        if (c == ' ') {
	            str.setCharAt(P2--, '0');
	            str.setCharAt(P2--, '2');
	            str.setCharAt(P2--, '%');
	        } else {
	            str.setCharAt(P2--, c);
	        }
	    }
	    return str.toString();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值