数据结构-------数组

数据结构:

1.1 数组:

实现一个支持动态扩容的数组
public class MyArrayList<T> {
	
	public MyArrayList(){}
	Object[] arrays=new Object[1];
	int count = 0;
	public Object[] getArrays() {
		return arrays;
	}

	public void setArrays(Object[] arrays) {
		this.arrays = arrays;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}
	
	//扩容2n+1
	public void ensureCapacity(int length) {
		Object[] newarrays=new Object[length];
		for (int i = 0; i < arrays.length-1; i++) {
			newarrays[i]=arrays[i];
		}
		arrays=newarrays;
	}
	
	//增
	public boolean add(T element) {
		if (arrays.length<count+1) {
			ensureCapacity(arrays.length*2+1);
		}
		arrays[count]=element;
		count++;
		return true;
	}
	
	//删
	public boolean remove(int local) {
		T removeItem = (T) arrays[local];
		for (int i = local; i < arrays.length-1; i++) {
			arrays[i]=arrays[i+1];
		}
		count--;
		return true;
	}
	
	//改
	public boolean set(int local , T element) {
		arrays[local]=element;
		return true;
	}
	
	//查
	public T get(int local) {
		if (local<arrays.length) {
			return (T)arrays[local];
		}else {
			return null;
		}
	}
	
	public String toString() {
		return Arrays.toString(arrays);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyArrayList<String> arrayList=new MyArrayList<String>();
		
		//实现一个支持动态扩容的数组
		arrayList.add("1");
		arrayList.add("2");
		arrayList.add("3");
		System.out.println(arrayList.toString());
	
	}
}
实现一个大小固定的有序数组,支持动态增删改操作,实现两个有序数组合并为一个有序数组
public class SortArray {

	int[] arrays;
	int count;
	
	public SortArray(){}
	
	public SortArray(int length){
		arrays=new int[length];
	}
	
	public int[] getArrays() {
		return arrays;
	}

	public void setArrays(int[] arrays) {
		this.arrays = arrays;
	}
	
	//增
	public void add(int element) {
		
		if (arrays.length>count) {
			
			arrays[count]=element;
			sort(arrays, 0, count);
			count++;
		}
		
	}
	
	//删
	public boolean remove(int local) {
		int removeItem =  arrays[local];
		for (int i = local; i < arrays.length-1; i++) {
			arrays[i]=arrays[i+1];
		}
		count--;
		
		return true;
	}
	
	//改
	public boolean set(int local , int element) {
		
		arrays[local]= element;
		sort(arrays, 0, count-1);
		return true;
	}
	
	public boolean merge(int[] a,int[] b) {
		
		int[] temp=new int[a.length+b.length];
		
		for (int i = 0; i < temp.length; i++) {
			if (i<a.length) {
				temp[i]=a[i];
			}else {
				temp[i]=b[i-a.length];
			}
		}
		count=temp.length;
		arrays=temp;
		sort(arrays, 0, count-1);
		
		return true;
	}
	
	private static void sort(int[] temp, int low, int high) {
		//1,找到递归算法的出口
		if( low > high) {
			return;
		}
		//2, 存
		int i = low;
		int j = high;
		//3,key
		int key = temp[low];
		//4,完成一趟排序
		while( i< j) {
			//4.1 ,从右往左找到第一个小于key的数
			while(i<j && temp[j] > key){
				j--;
			}
			// 4.2 从左往右找到第一个大于key的数
			while( i<j && temp[i] <= key) {
				i++;
			}
			//4.3 交换
			if(i<j) {
				int p = temp[i];
				temp[i] = temp[j];
				temp[j] = p;
			}
		}
		// 4.4,调整key的位置
		int p = temp[i];
		temp[i] = temp[low];
		temp[low] = p;
		//5, 对key左边的数快排
		sort(temp, low, i-1 );
		//6, 对key右边的数快排
		sort(temp, i+1, high);
	}

	public String toString() {
		
		return Arrays.toString(arrays);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//实现一个大小固定的有序数组,支持动态增删改操作
		SortArray sa=new SortArray(9);
		
		sa.add(1);
		sa.add(6);
		sa.add(14);
		sa.add(4);
		sa.add(11);
		sa.add(13);
		sa.add(9);
		System.out.println(sa.toString());
		
		sa.remove(3);
		System.out.println(sa.toString());
		
		sa.set(2, 7);
		System.out.println(sa.toString());
		
		//实现两个有序数组合并为一个有序数组
		SortArray sb=new SortArray(5);
		sb.add(3);
		sb.add(4);
		sb.add(2);
		sb.add(10);
		sb.add(1);
		
		sa.merge(sa.arrays, sb.arrays);
		System.out.println(sa.toString());
	}

}

LeetCode

题目:
编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。

示例: 

输入: 19
输出: true
解释: 
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
代码:
class Solution {
    public boolean isHappy(int n) {
      
		Map<Integer, Integer> map=new HashMap<Integer, Integer>();
		
		Map<Integer, Integer> resultMap=new HashMap<Integer, Integer>();
		ArrayList<Integer> resultList=new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			map.put(i, (int) Math.pow(i, 2));
		}
		
		while (true) {
		
			ArrayList<Integer> list=new ArrayList<Integer>();
			int temp=n;
			int sum=0;
			while (temp!=0) {
				list.add(temp % 10);
				temp /= 10;
			}
			
			for (Integer integer : list) {
				sum+=map.get(integer);
			}
			
			if (sum==1) {
				
				return true;
			}else {
				if (!resultList.contains(sum)) {
					resultList.add(sum);
					n=sum;
				}else {
					
					return false;
				}
			}
		}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值