随笔分类 -  算法

记录LeetCode、牛客网等算法题解题过程。
摘要:题意:给定一个数组,数组的度是其中出现最多次数的元素。求,最小连续子数组的度和原数组一致。 思路:(参考最佳答案) 遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引、最后一次出现的索引、出现次数。 阅读全文
posted @ 2018-05-15 21:44 何以解忧,唯有*代码 阅读(207) 评论(0) 推荐(0)
摘要:Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) Yo 阅读全文
posted @ 2018-05-15 20:44 何以解忧,唯有*代码 阅读(142) 评论(0) 推荐(0)
摘要:Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decr 阅读全文
posted @ 2018-05-14 22:10 何以解忧,唯有*代码 阅读(120) 评论(0) 推荐(0)
摘要:Given an integer array, find three numbers whose product is maximum and output the maximum product. 题目:给定一个数组,数组长度3-10000,求其中三个数乘积最大的值;(最大乘积不会超过int范围) 阅读全文
posted @ 2018-05-14 14:19 何以解忧,唯有*代码 阅读(127) 评论(0) 推荐(0)
摘要:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. 题目:给一个无序数组,长度为n,元素为从0到n,缺少一个,求缺了 阅读全文
posted @ 2018-05-09 21:06 何以解忧,唯有*代码 阅读(109) 评论(0) 推荐(0)
摘要:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the arr 阅读全文
posted @ 2018-05-05 14:31 何以解忧,唯有*代码 阅读(105) 评论(0) 推荐(0)
摘要:Given an array, rotate the array to the right by k steps, where k is non-negative. 题目:给定一个数组和一个数值k,要求将数组向右旋转k步。 以前准备面试时看到过这个题目,结果今天还是记不得空间复杂度O(1)的解法,好 阅读全文
posted @ 2018-05-05 14:09 何以解忧,唯有*代码 阅读(121) 评论(0) 推荐(0)
摘要:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the 阅读全文
posted @ 2018-05-05 13:34 何以解忧,唯有*代码 阅读(84) 评论(0) 推荐(0)
摘要:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function 阅读全文
posted @ 2018-05-05 12:03 何以解忧,唯有*代码 阅读(121) 评论(0) 推荐(0)
摘要:118 题目:又叫“杨辉三角形” Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum o 阅读全文
posted @ 2018-05-03 22:11 何以解忧,唯有*代码 阅读(150) 评论(0) 推荐(0)
摘要:题目:将两个有序数组合并成一个 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized 阅读全文
posted @ 2018-05-03 21:32 何以解忧,唯有*代码 阅读(153) 评论(0) 推荐(0)
摘要:题目: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significa 阅读全文
posted @ 2018-05-02 20:56 何以解忧,唯有*代码 阅读(664) 评论(0) 推荐(0)
摘要:求连续子数组的最大和,这道题是面试经典问题。《算法导论》中提供了分治的解法,但是DP解法是最快的, 能达到O(N)。 思路:若当前的和小于0,则应该抛弃当前的和,重新开始求和。有一些边界条件需要注意: 1.数组长度为1时,直接返回该值; 2.数组元素均为负数时,问题就变成了找数组的最大元素。需要判断 阅读全文
posted @ 2018-05-02 19:57 何以解忧,唯有*代码 阅读(105) 评论(0) 推荐(0)
摘要:题目:Given a string, find the length of the longest substring without repeating characters. 思路:用一个表(数组)来存储字符在字符串中的位置。因为字符本质上是一个unicode数字,因此建立一个数组,数组的下标表 阅读全文
posted @ 2018-04-26 14:55 何以解忧,唯有*代码 阅读(119) 评论(0) 推荐(0)
摘要:题目: Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra 阅读全文
posted @ 2018-04-26 09:32 何以解忧,唯有*代码 阅读(104) 评论(0) 推荐(0)
摘要:题目: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes co 阅读全文
posted @ 2018-04-26 08:33 何以解忧,唯有*代码 阅读(154) 评论(0) 推荐(0)