[US Giants] 二. Integer-Array
来源:互联网 发布:淘宝卖家快递推荐 编辑:程序博客网 时间:2024/06/03 21:47
Remove Element
Given an array and a value, remove all occurrences of that value in place and return the new length.
The order of elements can be changed, and the elements after the new length don't matter.
Given an array [0,4,4,0,0,2,4,4]
, value=4
return 4
and front four elements of the array is [0,0,0,2]
方法一
思路:i从开始遍历,只要是不等于elem的元素值,就挨着从index=0开始从前往后放,后面的就是都等于elem的public class Solution { public int removeElement(int[] A, int elem) { int index=0; for(int i=0;i<A.length;i++){ if(A[i]!=elem){ A[index++]=A[i]; } } return index; }}
方法二
思路:i从前往后遍历,如果A[i]=elem,就和最后一个交换(即使最后一个也等于i也没关系,因为和下一个判断的时候还是要从i开始)
如果不等于,就继续判断下一个
public class Solution { /** *@param A: A list of integers *@param elem: An integer *@return: The new length after remove */ public int removeElement(int[] A, int elem) { if(A==null || A.length==0){ return 0; } int i=0; int j=A.length-1; while(i<=j){ if(A[i]==elem){ A[i]=A[j]; j--; }else{ i++; } } return i; }}
50. Product of Array Exclude Itself
点击打开链接
思路:两趟循环,对于每一个output[i]={i前面的数的乘积}*{i后面的数的乘积}
第一趟正向遍历数组,对于每个Xi计算Xo~Xi-1的连续乘积
第二趟反向遍历数组,对于每个Xi计算Xn-1~Xi+1的连续乘积
例如:A={2,3,4};
left={1,2,6}; left[1]=left[0]*2=2; left[2]=left[1]*3=6
right={12,4,1}; right[2]=1; right[1]=right[2]*4=4; right[0]=right[1]*3=12
output={12,8,6};
public class Solution { /** * @param A: Given an integers array A * @return: A Long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1] */ public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) { ArrayList<Long> result = new ArrayList<Long>(); if (A == null || A.size() == 0) { return result; } Long[] left = new Long[A.size()]; left[0] = 1L; for (int i = 1; i < A.size(); i++) { left[i] = left[i - 1] * A.get(i - 1); } Long[] right = new Long[A.size()]; right[A.size() - 1] = 1L; for (int i = A.size() - 2; i >= 0; i--) { right[i] = right[i + 1] * A.get(i + 1); } for (int i = 0; i < A.size(); i++) { result.add(left[i] * right[i]); } return result; }}
189. First Missing Position
public class Solution { /** * @param A: an array of integers * @return: an integer */ public int firstMissingPositive(int[] A) { if(A==null || A.length==0){ return 1; } for(int i=0;i<A.length;i++){ while(A[i]>0 && A[i]<=A.length && A[i]!=i+1){ int temp=A[A[i]-1]; //temp表示当前i应该在的位置的值,因为当前i最后会被交换到应该在的位置 if(temp==A[i]){ //例如[-1,-1,1],遍历到i=2的时候,A[i]=1,而temp=A[0]=-1 break; } A[A[i]-1]=A[i]; A[i]=temp; } } for(int i=0;i<A.length;i++){ if(A[i]!=i+1){ return i+1; } } return A.length+1; }}
- [US Giants] 二. Integer-Array
- [US Giants] 一. String
- [US Giants] 五. Greedy
- [US Giants] 三. Binary Search
- [US Giants] 六. Linked List
- [US Giants] 七. Binary Tree
- [US Giants] 八. Search & Recurison
- [US Giants] 十. Data Structure
- [US Giants] 四.Math & Bit Manipulation
- Java integer to byte array
- LintCode天梯(USGiants)-Integer Array
- 一个整形(Integer)数组(Array)的Utility
- missing ranges of sorted integer array
- 13. 常见类 (StringBuffer、Array、Integer、Character)
- android数组资源string-array integer-array的用法
- numpy深入理解(2) indexing: integer array & boolean array
- Android简单类型资源(Bool、Color、Dimension、ID、Integer、Integer Array、Typed Array)
- Android简单类型资源(Bool、Color、Dimension、ID、Integer、Integer Array、Typed Array)
- Google Guava--让java代码更加优雅
- PAT 1044火星数字
- UCOSii-信号量
- C语言指针专题——使用指针要注意这些
- json解析——fastjson
- [US Giants] 二. Integer-Array
- Java语言为什么可以跨平台
- 谷歌教机器人理解语义,像人一样学习复杂技能 | 附3篇论文
- Linux内存管理机制
- SpringMCV运行流程及整合spring
- Java面试知识总结
- 英语六级备考策略
- 1106: 回文数(函数专题)
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】