一些编程题目的解析

附上我的github仓库,会不断更新leetcode解题答案,提供一个思路,大家共勉

希望可以给star,鼓励继续更新解题思路

author: thomas

这个是leetcode上面的编程题目

Leetcode之javascript解题(No442/No485/No561/No566)

No442. Find All Duplicates in an Array(Medium)

题目

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]
复制代码

这道题给了我们一个数组,数组中的数字可能出现一次或两次,让我们找出所有出现两次的数字

思路

  • 这类问题的一个重要条件就是1 ≤ a[i] ≤ n (n = size of array),不然很难在O(1)空间和O(n)时间内完成。
  • 方法一:正负替换的方法
    • 这类问题的核心是就是找nums[i]和nums[nums[i] - 1]的关系,我们的做法是,对于每个nums[i],我们将其对应的nums[nums[i] - 1]取相反数(因为坐标是从0开始)如果其已经是负数了,说明之前存在过,我们将其加入结果res中即可
    • 当然我们每次取值的时候是取当前的绝对值来给对应的位置取负数Math.abs(elem) - 1;
  • 方法二:遍历数组,用lastIndexOf方法,只要从后查找当前值的位置和当前不同,那就是我们取的值。arr.lastIndexOf(elem) !== i)
  • 方法三(最优解):
    • nums[nums[i]-1]位置累加数组长度n,(注意nums[i]-1有可能越界),所以我们需要对n取余,最后要找出现两次的数只需要看nums[i]的值是否大于2n即可,最后遍历完nums[i]数组为[12, 19, 18, 15, 8, 2, 11, 9],我们发现有两个数字19和18大于2n,那么就可以通过i+1来得到正确的结果2和3了

代码

// 方法一
  let arr = [4,3,2,7,8,2,3,1];
  var findDuplicates = function(arr) {
		let res = [];
		arr.forEach((elem, i) => {
		  let temp = Math.abs(elem) - 1;
		  if (arr[temp] < 0) {
		    res.push(temp + 1); // 取的负值位置+1
			}
			arr[temp] = -arr[temp]
		})
		return res;
 };
 
 // 方法三
 var findDuplicates3 = function(arr) {
    let res = [],
		len = arr.length;
    for (let i = 0; i < len; ++i) {
      arr[(arr[i] - 1) % len] += len; // 对len取余,防止越界
    }
    for (let i = 0; i < len; ++i) {
      if (arr[i] > 2 * len) res.push(i + 1);
    }
    return res;
  };

 console.log(findDuplicates3(arr));
复制代码

No485:Max Consecutive Ones(Easy)

题目

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.
复制代码
  • Note:

The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000

思路

这道题让我们求最大连续1的个数,不是一道难题。我们可以遍历一遍数组,用一个计数器cnt来统计1的个数,方法是如果当前数字为0,那么cnt重置为0,如果不是0,cnt自增1,然后每次更新结果res即可

代码

/**
 * Created by zhoubowen on 2018/4/10.
 *
 * No 485. Max Consecutive Ones
 */
let arr = [1,1,0,1,1,1];
let findMaxconsecutiveOnes = function(arr) {
  let max = 0,
    count = 0;
  arr.forEach((elem, i) => {
    if (elem === 1) {
      count += 1;
    }else {
      if (count > max) {
        max = count;
      }
      count = 0;
    }
  });
  // 单独判断以下最后一个结果的情况
  max = count > max ? count:max;
  return max;
};
console.log(findMaxconsecutiveOnes(arr));
复制代码

No561:Array Partition I(Easy)

题目

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

  • Example 1:
Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
复制代码
  • Note:

n is a positive integer, which is in the range of [1, 10000]. All the integers in the array will be in the range of [-10000, 10000].

意思是:给一个2n长度的数组,两两为一组,然后取每组的最小值,最后对所有组的min值求和,问如何匹配保证这个求和的值最大

思路

就是给2n个数分组,两两一组,使用所有组中小的那个数加起来和最小。

既然我们两两分组,并且只取最小的那个值,那两个值中较大的值就浪费了。为了使浪费最低,那就每个分组都保证大的那个值只比小的那个值大一点点(也就是最接近小的那个值)。

先将所有数排序,然后就是以排序后的值从前往后每2个值为一组(这样就保证每个组中大的值最接近小的值),由于每个分组都是最小的值,所以我们取的值就是位置1,3,5...的值

代码

//
let arr = [1,4,3,2];
var arrayPariSum = function(arr) {
  let first = 1,
    end = arr.length;
  arr = QuickSort(arr, first, end); // 这是调用快排函数
  let len = arr.length,
    sum = 0;
  for (let i = 0; i < len; i += 2) { //只取1,3,5..位置的值相加
    sum += arr[i];
  }
  return sum;
};
console.log(arrayPariSum(arr));
复制代码

No566:Reshape the Matrix(Easy)

题目

题目:给一个二维数组和两个数字,返回一个二维数组,第一个数字代表返回的数组的行,第二个数字代表列。

Example 1:
Input: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
Output: 
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
复制代码

Example 2:

Input: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
Output: 
[[1,2],
 [3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
复制代码

思路

  • 刚开始想逻辑想不明白,可能是想循环一下搞定,但是不行,只能在循环外面创建两个变量来控制要返回的数组的接收:
  • javascript在多维数组的创建上和其他语言不同,没有多维数组,所以只能自己不断地在内部创建新的数组(用到的时候,再创建)

代码

<script>
    let arr = [[1,2],[3,4],[5,6]];
	let r = 2, c = 3;
	let matrixReshape = function(arr, r, c) {
	  if (arr === null) {
	    return false;
	  }
	  if (arr.length * arr[0].length !== r * c) {
	    return arr;
	  }
	  let [tempr, tempc,row, col] = [0, 0, arr.length, arr[0].length],
	  res = [];
      res[tempr] = [];// 这里要先在数组res中创建一个新的数组
	  for (let i = 0; i < row; i++) {
	    for (let j = 0; j < col; j++) {
	      res[tempr][tempc] = arr[i][j];
	      if (tempc === c-1) { // 第一行满了
	        tempr += 1;
	        if (tempr < r) {
              res[tempr] = [];// 如果满足条件,在内部多创建一个空数组
			}
	        tempc = 0;
			continue;
		  }
		  tempc += 1;
		}
	  }
	  return res;
	}
	console.log(matrixReshape(arr, r, c))
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值