
Array
文章平均质量分 72
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Design a Stack With Increment Operation
Design a stack which supports the following operations.Implement theCustomStackclass:CustomStack(int maxSize)Initializes the object withmaxSizewhich is the maximum number of elements in the stack or do nothing if the stack reached themaxSize. vo...原创 2022-03-15 13:45:50 · 193 阅读 · 0 评论 -
Valid Mountain Array
Given an arrayAof integers, returntrueif and only if it is avalid mountain array.Recall that A is a mountain array if and only if:A.length >= 3 There exists someiwith0 < i< A.length - 1such that: A[0] < A[1] < ... A[i-1] <...原创 2020-06-25 13:25:30 · 213 阅读 · 0 评论 -
RLE Iterator
Write an iterator that iterates through a run-length encoded sequence.The iterator is initialized byRLEIterator(int[] A), whereAis a run-length encoding of somesequence. More specifically,for all eveni,A[i]tells us the number of times that the n...原创 2020-06-15 13:17:13 · 204 阅读 · 0 评论 -
Decompress Run-Length Encoded List
We are given a listnumsof integers representing a list compressed with run-length encoding.Consider each adjacent pairof elements[freq, val] = [nums[2*i], nums[2*i+1]](withi >= 0). For each such pair, there arefreqelements with valuevalconc...原创 2020-06-08 06:26:32 · 193 阅读 · 0 评论 -
Exam Room
In an exam room, there areNseats in a single row, numbered0, 1, 2, ..., N-1.When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the l...原创 2020-05-31 07:42:01 · 614 阅读 · 0 评论 -
Add Bold Tag in String && Bold Words in String
Given a stringsand a list of stringsdict, you need to add a closed pair of bold tag<b>and</b>to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold t...原创 2020-05-26 09:23:52 · 220 阅读 · 0 评论 -
Non-decreasing Array
Given an arraynumswithnintegers, your task is to check if it could become non-decreasing by modifyingat most1element.We define an array is non-decreasing ifnums[i] <= nums[i + 1]holds for everyi(0-based) such that(0<= i <= n - 2)....原创 2020-05-11 07:37:37 · 207 阅读 · 0 评论 -
Subtract the Product and Sum of Digits of an Integer
Given an integer numbern, return the difference between the product of its digits and the sum of its digits.Example 1:Input: n = 234Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15思路.原创 2020-05-10 02:21:43 · 178 阅读 · 0 评论 -
Number of Steps to Reduce a Number to Zero
Given a non-negative integernum, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.Example 1:Input: num = 14Output: 6Explanation:Step 1) 14 is eve..原创 2020-05-10 02:15:01 · 278 阅读 · 0 评论 -
Toeplitz Matrix
A matrix isToeplitzif every diagonal from top-left to bottom-right has the same element.Now given anM x Nmatrix, returnTrueif and only if the matrix isToeplitz.Example 1:Input:matrix = ...原创 2020-04-11 04:02:34 · 449 阅读 · 0 评论 -
Minimum Increment to Make Array Unique
Given an array of integers A, amoveconsists of choosing anyA[i], and incrementing it by1.Return the least number of moves to make every value inAunique.Input: [3,2,1,2,1,7] Output: 6 Explanati...原创 2020-03-29 05:56:19 · 135 阅读 · 0 评论 -
Find the Town Judge
In a town, there areNpeople labelled from1toN. There is a rumor that one of these people is secretly the town judge.If thetown judge exists, then:The town judge trusts nobody. Everybody (e...原创 2020-02-11 15:30:34 · 150 阅读 · 0 评论 -
Intersection of Arrays
Give a number of arrays, find their intersection, and output their intersection size.ExampleExample 1: Input: [[1,2,3],[3,4,5],[3,9,10]] Output: 1 Explanation: Only '3' in all three arra...原创 2019-11-04 05:59:40 · 148 阅读 · 0 评论 -
Maximum Index
Given an array arr[], find the maximum j – i such that arr[j] > arr[i]Given an array arr[], find the maximum j – i such that arr[j] > arr[i].Examples: Input: {34, 8, 10, 3, 2, 80, 30, 33,原创 2016-09-13 10:26:03 · 404 阅读 · 0 评论 -
Array Partition I
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 poss...原创 2018-08-10 13:20:05 · 450 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may no...原创 2014-12-20 09:56:48 · 422 阅读 · 0 评论 -
Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [...原创 2016-06-29 13:35:40 · 223 阅读 · 0 评论 -
Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note:The lengt...原创 2018-08-12 12:43:49 · 269 阅读 · 0 评论 -
Majority Element
Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.You may assume that the array is non-empty and the majority element alwa...原创 2015-01-15 15:01:29 · 504 阅读 · 0 评论 -
Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Hint:How many majority elements could it possibly...原创 2016-08-15 13:02:07 · 272 阅读 · 0 评论 -
Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Coul...原创 2018-08-08 11:34:28 · 195 阅读 · 0 评论 -
Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤n(n= size of array), some elements appeartwiceand others appearonce.Find all the elements that appeartwicein this array.Could you do it without extra ...原创 2018-08-08 13:04:56 · 176 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3]....原创 2014-01-02 15:51:45 · 479 阅读 · 0 评论 -
Best Time to Buy and Sell Stock I
Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock)...原创 2016-10-16 02:25:06 · 230 阅读 · 0 评论 -
Product of Array Except Self
Given an array ofnintegers wheren> 1,nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Solve itwithout divisionand in O(n)....原创 2016-07-23 02:33:38 · 348 阅读 · 0 评论 -
Shortest Word Distance III
This is afollow upofShortest Word Distance. The only difference is nowword1could be the same asword2.Given a list of words and two wordsword1andword2, return the shortest distance betwe原创 2016-09-10 04:45:32 · 355 阅读 · 0 评论 -
Missing Ranges
Given a sorted integer array where the range of elements are [lower,upper] inclusive, return its missing ranges.For example, given[0, 1, 3, 50, 75],lower= 0 andupper= 99, return["2", "4->49原创 2014-12-10 13:49:55 · 2458 阅读 · 0 评论 -
Trapping Rain water
Givennnon-negative integers representing an elevation map where the width of each bar is1, compute how much water it is able to trap after raining.ExampleExample 1:Input: [0,1,0]Output: 0...原创 2016-09-14 11:29:38 · 500 阅读 · 0 评论 -
Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:上下左右替换,layer 注意offset = i - first;是算与前面的差距。然后就原创 2013-12-31 10:34:50 · 612 阅读 · 0 评论 -
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You原创 2014-02-10 14:17:50 · 575 阅读 · 0 评论 -
Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.思路:用第一行,第一列来记录信息。然后首先略过第一行第一列,进行设置。然后再看第一行第一列是否要设置0;public class Solution { public原创 2014-02-07 05:31:50 · 621 阅读 · 0 评论 -
Merge Sorted Array
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may assume tha...原创 2014-01-27 14:37:12 · 645 阅读 · 0 评论 -
Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.T...原创 2014-01-19 07:41:46 · 559 阅读 · 0 评论 -
Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.思路:刚开始想的...原创 2016-07-11 15:10:46 · 248 阅读 · 0 评论 -
Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 ton2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2014-02-10 14:31:13 · 476 阅读 · 0 评论 -
Find the Celebrity
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/s...原创 2016-08-25 14:25:23 · 351 阅读 · 0 评论 -
Shortest Word Distance
Given a list of words and two wordsword1andword2, return the shortest distance between these two words in the list.For example,Assume that words =["practice", "makes", "perfect", "coding", "原创 2016-07-02 13:09:56 · 427 阅读 · 0 评论 -
Read N Characters Given Read4 II - Call multiple times
The API:int read4(char *buf)reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the原创 2014-12-12 13:14:20 · 3892 阅读 · 0 评论 -
Read N Characters Given Read4
The API:int read4(char *buf)reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the原创 2014-12-12 12:09:11 · 6321 阅读 · 0 评论 -
Valid sudoku
Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially filled sud...原创 2014-03-04 15:43:22 · 570 阅读 · 0 评论