Array
Crystal_ting
个人博客 limengting.site
展开
-
leetcode448 Find All Numbers Disappeared in an Array
package array;import java.util.ArrayList;public class leecode448FindNumbersNotAppear { /*448. Find All Numbers Disappeared in an ArrayGiven an array of integers where 1 ≤ a[i] ≤ n (n = size o...原创 2018-08-16 15:29:43 · 327 阅读 · 0 评论 -
64. Minimum Path Sum(Java)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any原创 2017-09-28 00:53:16 · 321 阅读 · 0 评论 -
[3]118. Pascal's Triangle(Java)
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]// 错误程序class Solution { public L原创 2017-09-26 22:29:45 · 465 阅读 · 0 评论 -
[3]48. Rotate Image(Java)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Example 1:Input: k = 3, n =原创 2017-09-06 21:13:58 · 327 阅读 · 0 评论 -
[4]380. Insert Delete GetRandom O(1)(Java)
Design a data structure that supports all following operations in average O(1) time.1、insert(val): Inserts an item val to the set if not already present. 2、remove(val): Removes an item val from the se原创 2017-09-26 11:51:11 · 349 阅读 · 0 评论 -
46. Permutations/47. Permutations II(Java)
https://discuss.leetcode.com/topic/46159/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioning46. PermutationsGiven a collection of distinct原创 2017-09-08 18:43:26 · 286 阅读 · 0 评论 -
[4]78. Subsets/90. Subsets II(Java)
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:[ [3], [1], [2], [1,原创 2017-09-08 17:04:59 · 388 阅读 · 0 评论 -
27. Remove Element(C++/Java)
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.The order原创 2017-07-04 16:05:17 · 303 阅读 · 0 评论 -
670. Maximum Swap(Java)
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.Example 1:Input: 2736Output: 7236Explanation: Swa原创 2017-09-28 23:13:07 · 560 阅读 · 0 评论 -
674. Longest Continuous Increasing Subsequence(Java)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence.Example 1:Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing subsequence is [1原创 2017-09-21 22:15:50 · 300 阅读 · 0 评论 -
[4]560. Subarray Sum Equals K(Java)
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note: The length of the arra原创 2017-09-21 23:44:01 · 351 阅读 · 0 评论 -
leetcode169 Majority Element
package array;import java.util.HashMap;public class leetcode169MajorityElement { /* Given an array of size n, find the majority element. The majority element is the element that appear...原创 2018-08-16 15:28:34 · 273 阅读 · 0 评论 -
leetcode283 Move Zeros
package array;/*Given an array numswrite a function to move all 0's to the end of itwhile maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,...原创 2018-08-16 15:27:04 · 417 阅读 · 0 评论 -
《剑指offer》把一棵二叉树打印成多行
题目描述 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。import java.util.ArrayList;import java.util.Queue;import java.util.LinkedList;/*public class TreeNode { int val = 0; TreeNode left = null; Tree...原创 2018-04-30 12:21:19 · 236 阅读 · 0 评论 -
287. Find the Duplicate Number / 142. Linked List Cycle II(Java)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi...原创 2017-08-11 20:40:44 · 226 阅读 · 0 评论 -
《剑指offer》11.旋转数组的最小数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。思路:利用数组两部分都是有序的进行二分查找,时间复杂度O(logn)import java.util.Array原创 2018-03-26 19:11:57 · 149 阅读 · 0 评论 -
《剑指offer》3.数组中重复的数字
原题链接:3.数组中重复的数字题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。思路: 找数组中其中一个重复的数字/全部重复数字:①HashMap ②如果n个数字...原创 2018-03-22 23:23:21 · 253 阅读 · 0 评论 -
[3]43. Multiply Strings(Java)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:1、 The length of both num1 and num2 is < 110.2、 Both num1 and num2 contains only digits原创 2017-11-07 22:09:14 · 361 阅读 · 0 评论 -
[3]153. Find Minimum in Rotated Sorted Array(Java)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exist原创 2017-09-22 14:15:57 · 228 阅读 · 0 评论 -
?最大子列和问题(Java)/[3]53. Maximum Subarray(Java)
算法1:二重循环,复杂度O(n²)import java.util.Scanner;public class MaxSubSum1 { public static int maxSubSum(int[] array) { int thisSum, maxSum = 0; for (int i = 0; i < array.length; i ++) {原创 2017-07-30 18:53:54 · 333 阅读 · 0 评论 -
35. Search Insert Position(Java)
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here原创 2017-09-25 12:24:53 · 322 阅读 · 0 评论 -
[3]565. Array Nesting(Java)
A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].Sets S[K] for 0 <= K < N are defined as follows:S[K] = { A[K], A[A[K]], A[A[A原创 2017-09-05 19:07:25 · 1084 阅读 · 0 评论 -
[3]442. Find All Duplicates in an Array/[3]448. Find All Numbers Disappeared in an Array(Java)
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原创 2017-09-04 21:13:20 · 260 阅读 · 0 评论 -
667. Beautiful Arrangement II(Java)
Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, … , an原创 2017-09-04 22:25:31 · 650 阅读 · 0 评论 -
283. Move Zeroes(C++/Java)
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct原创 2017-07-11 18:32:28 · 244 阅读 · 0 评论 -
495. Teemo Attacking(Java)
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time原创 2017-09-04 22:11:21 · 340 阅读 · 0 评论 -
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-09-04 21:33:51 · 235 阅读 · 0 评论 -
566. Reshape the Matrix(Java)
In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dimensio原创 2017-08-11 17:09:42 · 1077 阅读 · 0 评论 -
561. Array Partition I(Java)
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 possibl原创 2017-08-11 16:57:34 · 438 阅读 · 0 评论 -
[3]122. Best Time to Buy and Sell Stock II(Java)
Say you have an array for which the i-th element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one an原创 2017-07-14 16:05:30 · 385 阅读 · 0 评论 -
661. Image Smoother(Java)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrou原创 2017-09-06 18:36:51 · 566 阅读 · 0 评论 -
[3]217. Contains Duplicate(Java)
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 array, and it should return false if every element is原创 2017-08-02 21:26:27 · 235 阅读 · 0 评论 -
[4]62. Unique Paths(Java)
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto原创 2017-09-07 21:46:00 · 335 阅读 · 0 评论 -
[3]611. Valid Triangle Numbe(Java)
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example 1:Inpu原创 2017-09-08 11:20:36 · 276 阅读 · 0 评论 -
[4]54. Spiral Matrix/59. Spiral Matrix II(Java)
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 should r原创 2017-09-25 00:19:28 · 407 阅读 · 0 评论 -
[4]169. Majority Element/[4]121. Best Time to Buy and Sell Stock(Java)
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 array is non-empty and the majority element always原创 2017-07-14 17:15:08 · 280 阅读 · 0 评论 -
[4]121. Best Time to Buy and Sell Stock(Java)
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi原创 2017-09-07 15:13:12 · 335 阅读 · 0 评论 -
?[4]621. Task Scheduler(Java)
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be原创 2017-09-07 13:12:09 · 635 阅读 · 0 评论 -
268. Missing Number(Java)
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in line原创 2017-08-11 17:50:06 · 343 阅读 · 0 评论 -
628. Maximum Product of Three Numbers(Java)
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:1、The length of the given原创 2017-09-06 21:38:18 · 279 阅读 · 0 评论