leetcode题目解答报告(1)

Remove Element

题目:

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

题意:从数组中去除某个值,返回新数组的长度;注意在去除完毕后数组中的元素应该变化了,即出现value的位置都被替换掉了,最后一句的意思是 新长度后面的元素值任意,也就是说,新数组的总长度可以保持不变,但是新长度之前的元素要正确,后面的无所谓。

思路:

设置下标i,j,i表示遍历数组的下标,j表示当前比较的元素下标。初始j=0,从i=0开始遍历数组。如果a[i]==value,不执行任何操作,进行下一次遍历。,直到找到一个a[i]不等于value,这时将将a[i]赋给a[j],然后j自加一。直到遍历结束,此时j的值就是新的数组长度

public class RemoveElement {


	static public int quchu(int[] a,int value){
		int i=0,j=0;
		for(i=0;i<a.length;i++){//遍历数组
		if(a[i]==value)//如果a[i]等于value,跳过继续执行
		continue;
		a[j]=a[i];//如果不相等,赋值给a[j]
		j++;//j表示数组中与value不相等的元素个数
		}
		return j;

		}
		public static void main(String[] args){
		int[] a={1,2,3,3,4,5,3,6,3,7,3};
		int length=0;
		length=quchu(a,3);
		System.out.println(length);
		}


}

Remove Duplicates from Sorted Array

题目:

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

题意:
大意是将已排好序的数组中重复点删除,然后返回新数组的长度。数组中前n个数就是新的数组。唯一难点是不能用额外空间。
思路:
设置下标i,j,i表示遍历数组的下标,j表示当前比较的元素下标。初始j=0,从i=1开始遍历数组。如果a[i]==a[j],不执行任何操作,进行下一次遍历。,直到找到一个a[i]不等于a[j],这时将j加一,再将a[i]赋给a[j],把a[j]作为下一次比较的对象。直到遍历结束,此时j+1的值就是新的数组长度
public class RemoveDuplicatesfromSortedArray {
	public static int removeDuplicates(int A[]) {
		if(A.length == 0) {
		return 0;
		}
		int j = 0;
		/*
		 * 去重的数组A[0]不变,
		 * 查看A[1]是不是等于A[0],若等于,则继续查看A[2]是否等于A[0],直到找到一个不相等的A[i]
		 * 因为A[0]已被赋值,所以将A[i]的值赋给A[++j]
		 * 将新找到的数组元素作为比较对象继续遍历数组,直到结束
		 * 最后去重后的数组长度是j+1(A[0]-A[j]共j+1个)
		 * */
		for(int i = 1; i < A.length; i++) {
		if(A[j] != A[i]) {
			j++;//下标加一
		    A[j] = A[i];
		}
		}
		return j + 1;
		}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int length=0;
		int[] a={1,2,3,3,4,5,5,6,6,7,8,8,9};
		length=removeDuplicates(a);
		System.out.println(length);
		

	}

}


Remove Duplicates from Sorted Array II

题目:

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

题意:

每个数只允许重复一次,输出去重后的数字个数,依旧上一题的要求,常数空间也就是只能在原数组上操作。

思路:设置下标i,j,i表示遍历数组的下标,j表示当前比较的元素下标。初始j=0,从i=1开始遍历数组。如果a[i]==a[j],执行num加1操作,。之后判断num是否小于2,如果小于,则j自加1并把a[i]赋值给a[j]。若大于等于2,则不执行任何操作。如果a[i]!=a[j],则j自加1并把a[i]赋值给a[j],同时将num清零,进行下一次遍历,a[j]作为下一次比较的对象。直到遍历结束,此时j+1的值就是新的数组长度。


public class RemoveDuplicatesfromSortedArrayII {
	public static int removeDuplicates(int A[]) {
		if(A.length == 0) {
		return 0;
		}
		int j = 0;
		int num=0;//重复次数
		/*
		 * 去重的数组A[0]不变,
		 * 查看A[1]是不是等于A[0],若等于,num自加1,此时num=1,满足条件。继续查看A[2]是否等于A[0],若等于,则num=2,不满足                  *条件,之后所有重复的元素都被剔除。直到找到一个不相等的A[i]
		 * 因为A[0]已被赋值,所以将A[i]的值赋给A[++j],这时将num清零,用于统计新的元素相等的个数
		 * 将新找到的数组元素作为比较对象继续遍历数组,直到结束
		 * 最后去重后的数组长度是j+1(A[0]-A[j]共j+1个)
		 * */
		for(int i = 1; i < A.length; i++) {
			if(A[j]==A[i]) {
				num++;
				if(num<2) {
					j++;
					A[j]=A[i];
				}
			}
		    else  {
			j++;//下标加一
		    A[j] = A[i];
		    num=0;//清零,用于统计下一个元素的重复数
		}
		}
		return j + 1;
		}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int length=0;
		int[] a={1,2,3,3,3,4,5,5,5,6,6,6,7,8,8,9};
		length=removeDuplicates(a);
		System.out.println(length);
		

	}

}


Plus One

题目:

  Given a non-negative number represented as an array of digits, plus one to the number. 
  The digits are stored such that the most significant digit is at the head of the list. 

题意:

给定一个用数组表示的一个数,对它进行加一操作。 每一个数位都存储在数组的一个位置上。数组下标从大到小表示数位从低位到高位。

思路:

直接求解,设置一个进位标志carry,初值为1,表示加1,从最低位开始tmp = a[x] + carry, a[x] = tmp%10,carry = tmp/10,如果carry不为0对下一位再进行操作,直到所有的数位处理完或者carray为0就退出,如果最后还有carray不为0说明整个数组要扩展一个数位。  

public class PlusOne {
	
	public static int[] mytets(int[] a) {
		int carry=1;//进位值,初始为1,表示加1操作
		int temp;
		int i;
		for(i=a.length-1;i>=0;i--) {
			temp=a[i]+carry;
			carry=temp/10;//向下一位的进位值
			a[i]=temp%10;//当前为的结果值
			if(carry==0) {//无进位则退出
				break;
			}
		}
		if(carry==1) {//分析最后产生的进位,例如999的特殊情况
			int[] result=new int[a.length+1];
			System.arraycopy(a, 0, result, 1, result.length-1);
			result[0]=carry;
			return result;
		}
		else {
			return a;
		}
		
		
	}
	public static void main(String[] args) {
		int []a= {9,9,9,9,9};
		int result[]=mytets(a);
		for (int i = 0; i <result.length; i++) {
			System.out.print(result[i]);
		}
		
	}

}





原文地址:https://www.cnblogs.com/kangsir/p/6653293.html