218人阅读 评论(0)

# Array

## 27. Remove Element

https://leetcode.com/problems/remove-element/

public class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
int j = 0;
for(i = 0; i < nums.length; i++)
{
if(nums[i] == val)
{
continue;
}
else
{
nums[j] = nums[i];
j++;
}
}
return j;
}
}

public class Solution {
public int removeElement(int[] nums, int val) {
int len = nums.length;
for(int i = 0; i < len; i++)
{
if(nums[i] == val)
{
nums[i--] = nums[len-- -1];
}
}
return len;
}
}

## 26.Remove Duplicates from Sorted Array

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

public class Solution {
public int removeDuplicates(int[] nums) {
int len = nums.length;
int dup = 0;
for( int i = 0; i < len; )
{
int j = i+1;
for(; j < len && nums[i] == nums[j]; j++)
{
dup++;
continue;
}
i = j;
nums[i - dup-1] = nums[i-1];
}
return len-dup;
}
}

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0)
return 0;
int j = 0;
for(int i = 0; i < nums.length; i++)
if(nums[i]!=nums[j])
nums[++j] = nums[i];
return ++j;
}
}

## 80. Remove Duplicates from Sorted Array II

public class Solution {
public int removeDuplicates(int[] nums) {
int j = 0;
int dup = 0;
for ( int i = 0; i < nums.length; i++)
{
if(nums[i] != nums[j])
{
nums[++j] = nums[i];
dup = 0;
}
else if(dup == 0 && i!=j)//此处为了避免处理第一个元素时出问题，增加了一个i！=j的判断
{
dup = 1;
nums[++j] = nums[i];
}
}
return ++j;
}
}

public class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
int k = 2;

if( n <= k)
return n;
int i = 1, j = 1;
int cnt = 1;
while(i < n)
{
if(nums[i]!=nums[i-1])
{
cnt = 1;
nums[j++] = nums[i];
}
else
{
if(cnt < k)
{
nums[j++] = nums[i];
cnt++;
}
}
i++;
}
return j;
}
}

## 66. Plus One

https://leetcode.com/problems/plus-one/

public class Solution {
public int[] plusOne(int[] digits) {
int carry = 1;
int i = digits.length - 1;
while( i >= 0)
{
int sum = digits[i] + carry;
digits[i] = sum%10;
carry = sum/10;
if(carry == 0)
return digits;
i--;
}
int[] res = new int [digits.length+1];
res[0] = carry;
for(int j = 1; j < res.length; j++)
{
res[j] = 0;
}
return res;
}
}

public class Solution {
public int[] plusOne(int[] digits) {

int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}

digits[i] = 0;
}

int[] newNumber = new int [n+1];
newNumber[0] = 1;

return newNumber;
}

## 118. Pascal’s Triangle

Java中List的初始化一般用ArrayList

public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(numRows<=0)
return res;
for(int i = 0; i < numRows; i++)
{
List<Integer> newRow = new ArrayList<Integer>();
for(int j = 0; j <=i; j++)
{
if( j==0 || j == i)
else{
}
}
}
return res;
}
}

## 119. Pascal’s Triangle II

https://leetcode.com/problems/pascals-triangle-ii/

public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i <= rowIndex; i++){
for(int j = i-1; j > 0; j--){
res.set(j,res.get(j)+res.get(j-1));
}
}
return res;
}
}

## 2Sum

https://leetcode.com/problems/two-sum/

public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
for(int i = 0 ; i < nums.length; i++)
{
for(int j = i+1; j < nums.length; j++)
{
if(nums[i]+nums[j] == target)
{
res[0] = i;
res[1] = j;
}
}
}
return res;
}
}

public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0 ; i < nums.length; i++)
{
if(map.containsKey(target - nums[i]))
{
res[0] = map.get(target - nums[i]);
res[1] = i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}

## 3Sum

https://leetcode.com/problems/3sum/

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if(nums.length <= 2)
return new ArrayList<List<Integer>>();
Arrays.sort(nums);
Map<Integer,Integer> oneSum = new HashMap<Integer,Integer>();

Set<List<Integer>> resSet = new HashSet<List<Integer>>();
for(int i = 0; i < nums.length; i++)
{
oneSum.put(nums[i],i);
for(int j = i+1; j < nums.length; j++)
{
if(oneSum.containsKey(0 - nums[i]-nums[j]))
{
List<Integer> tmp = new ArrayList<Integer>();
}
}
}
List<List<Integer>> res = new ArrayList<List<Integer>>(resSet);
return res;
}
}

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<List<Integer>>();
for(int i = 0; i < nums.length-2; i++)
{
if(i == 0 || (i > 0 && nums[i]!=nums[i-1]))
{
int low = i+1, high = nums.length-1, sum = 0 - nums[i];
while(low < high)
{
if(nums[low] + nums[high] == sum)
{
while(low < high && nums[low] == nums[low+1])
low++;
while(low < high && nums[high] == nums[high-1])
high--;
low++;
high--;
}
else if(nums[low]+nums[high] < sum)
low++;
else
high--;
}
}
}
return res;
}
}

## 3Sum Closest

https://leetcode.com/problems/3sum-closest/

public class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int res = nums[0]+nums[1]+nums[2];
for(int i = 0; i < nums.length-2; i++)
{
int low = i+1, high = nums.length -1;
while(low < high)
{
int sum = nums[low]+nums[high]+nums[i];
if (sum < target)
{
low++;
}
else
{
high--;
}
if(Math.abs(target - sum) < Math.abs(target - res))
{
res = sum;
}
}
}
return res;
}
}

## Search a 2D Matrix

https://leetcode.com/problems/search-a-2d-matrix/

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0)
return false;
int row_num = matrix.length;
int col_num = matrix[0].length;

int begin = 0, end = row_num * col_num - 1;

while(begin <= end){
int mid = (begin + end) / 2;
int mid_value = matrix[mid/col_num][mid%col_num];
if( mid_value == target){
return true;
}else if(mid_value < target){
begin = mid+1;
}else{
end = mid-1;
}
}
return false;
}
}

public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0)
return false;
int clen = matrix[0].length;
int rlen = matrix.length;
int rnum = 0, cnum = clen-1;
while(rnum < rlen && cnum >= 0){
if(matrix[rnum][cnum] > target)
cnum--;
else if(matrix[rnum][cnum] < target)
rnum++;
else
return true;
}
return false;
}
}

## Find Peak Element

https://leetcode.com/problems/find-peak-element/

public class Solution {
public int findPeakElement(int[] nums) {
int low = 0, high = nums.length -1;
if(nums.length <= 1)
return 0;
while( low <= high){
int mid = (low+high)>>1;
if((mid==nums.length-1||nums[mid]>nums[mid+1])&&(mid==0||nums[mid]>nums[mid-1])){
return mid;
}
else if(nums[mid] < nums[mid+1]){
low = mid+1;
}else{
high = mid-1;
}
}
return low;
}
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1882次
• 积分：78
• 等级：
• 排名：千里之外
• 原创：6篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章存档
评论排行