Given an array of integers, find the numbers such that they add up to a specific target number.The function twoSum should return indices of the numbers such that they add up to the target, where index1 mus T is less than INDEX2. Please note that your returned answers (both Index1 and Index2) is not zero-based.NoticeYou may assume this each input would has exactly one solutionExamplenumbers= [2, 7, 11, 15] , target=9Return[1, 2]ChallengeEither of the following solutions is acceptable:
O
Write a program to find the nth super ugly number.Super ugly numbers is positive numbers whose all prime factors is in the given prime list primes of size k . For example, is the sequence of the first and [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] super ugly numbers given primes = of [2, 7, 13, 19] size 4 .Notice
1is a super ugly number for any given primes.
The given numbers in primes is in ascending order.
0
ExampleGiven n = 6 , primes = [2, 7, 13, 19] return13 Public c
Given a set of distinct integers, return all possible subsets.Notice
Elements in a subset must is in non-descending order.
The solution set must not contain duplicate subsets.
ExampleIf S = [1,2,3] , a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]ChallengeCan do it in both recursively and iteratively?classSolution {/** * @params:a set of numbers. * @return: A List of lists. All valid subsets. */ Publicarraylistint[] nums) { //Write yo
data to go heavy, feel that this is problematic, such as:-1-1 1 1, 1-1-1 3 This is also an answer, but the above nine chapters to the program is really by the deduplication operation ....Looking closely at the nine chapters of the procedure, found:1,i is the first number, J is the second number, start is the third number, end is the fourth number2. The first two numbers are de-weighed and the calculated start and end are also de-weighed, and start and end are well understood, but for the first
*/ Public intFirstmissingpositive (int[] A) {//Write your code here if(A.length = = 0) return1; intn =a.length; for(intI =0;i){ while(A[i]! = i + 1){ if(A[i] N) Break; intTMP =A[i]; A[i]= A[tmp-1]; A[tmp-1] =tmp; } } for(intI =0;i) if(A[i]! = i+ 1) returnI+1; returnn + 1; }}Java CodeclassSolution:#@param A, a list of integers #@return An integer deffirstmissingpositive (Self, A):#Write your code here
half of the minimum value of the second half of the maximum value, the subtraction is the answer, this seemingly difficult solution.At the time of the variable array, the minimum value in front of it is compared, and the current node can be regarded as the maximum value, the difference between the assumed maximum value and the minimum value of the previous one, and the profit obtained from the current node's sell, is the local maximum value.The maximum value of the local maximum is the overall
-target)?sum:tmpsum; if(Tmpdist = = 0){ returnTarget; } if(Tmpdist ) { left++; }Else{ Right--; } } } returnsum; }}Java CodeTotal time: 1504 MsclassSolution:"""@param numbers:give An array numbers of n integer @param target:an integer @return: Return the sum of T He three integers, the sum closest target. """ defthreesumclosest (self, Numbers, target):#Write your code here ifNumbers = =None:return0 Numlen=len (Numbers) number
sectional pruning, to adopt the means of cutting, in order to facilitate the branch, expand the canopy, cultivate strong high-yielding, stable production tree type."Kowloon White Tea" bud head fat, White is revealed, suitable for the production of white silver needles, maofeng, silver snail and other famous tea, the production of black tea, white tea quality is good. Therefore, in the mining system should adhere to the timely, batch tender picking principle to improve its yield and quality. The
Given a string containing only digits, restore it is returning all possible valid IP address combinations.ExampleGiven "25525511135" , return[ "255.255.11.135", "255.255.111.35"]Order does not matter. Public classSolution {/** * @params The IP string *@returnAll possible valid IP addresses*/ PublicArraylistrestoreipaddresses (String s) {//Write Your code hereArrayListNewArraylist(); String Line= ""; Helper (result, line,0, S, 0); returnresult; } Public voidHelper (arraylistintS
Given a singly linked list l:l0→l1→ ... →ln-1→lnReorder It to:l0→ln→l1→ln-1→l2→ln-2→ ...ExampleGiven 1->2->3->4->null , reorder it to 1->4->2->3->null .ChallengeCan do this in-place without altering the nodes ' values?/*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; * } * } */ Public classSolution {/** * @paramhead:the head of linked list. * @return: void*/ Public voidreorderlist (ListNode
] / \ / [1, 1, max=2], [2, 2, max=1], [3, 3, max=0], [4, 4, max=0]ChallengeDo it O(h) in time, and H is the height of the segment tree./*** Definition of Segmenttreenode: * public class Segmenttreenode {* Public int start, end, Max; * Public Segm Enttreenode left, right; * Public Segmenttreenode (int start, int end, int max) {* This.start = start; * This.end = end; * This.max = max * This.left = This.right = null; * } * } */ Public classSolu
You are given a n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).ExampleGiven a matrix[ [1,2], [3,4]]Rotate it by degrees (clockwise), return[ [3,1], [4,2]]ChallengeDo it in-place. Public classSolution {/** * @parammatrix:a List of lists of integers *@return: Void*/ Public voidRotateint[] matrix) { //Write your code here if(Matrix = =NULL|| Matrix.length ) return; intn =matrix.length; for(inti = 0; i ) rotate (
Given A list of numbers, return all possible permutations.ExampleFor nums = [1,2,3] , the permutations is:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]ChallengeDo it without recursion.classSolution {/** * @paramnums:a List of integers. * @return: A List of permutations. */ PublicArraylistnums) { //Write your code hereArrayListNewArraylist(); if(Nums = =NULL|| Nums.size () = = 0) returnresult; ArrayListNewArraylist(); Boolean[] visited =New Boolean[Nums.s
Given a list of integers, which denote a permutation.Find the previous permutation in ascending order.NoticeThe list may contains duplicate integers.Example[1,3,2,3]for, the previous permutation is[1,2,3,3][1,2,3,4]for, the previous permutation is[4,3,2,1] Public classSolution {/** * @paramnums:a List of integers *@return: A list of integers that ' s previous permuation*/ PublicArraylistnums) { //Write your code if(Nums = =NULL|| Nums.size () = = 0) returnNums; i
Given a permutation which may contain repeated numbers, find it index in all the permutations of these numbers, which is Ordered in lexicographical order. The index begins at 1.ExampleGiven the permutation [1, 4, 2, 2] , return 3 . Public classSolution {/** * @paramA an integer array *@returna long integer*/ Public LongPERMUTATIONINDEXII (int[] A) {//Write Your code here if(A = =NULL|| A.length = = 0) return1; HashMapNewHashmap(); for(inti = 0; i ){
Given A string s, cut s into some substrings such that every substring is a palindrome.Return the minimum cuts needed for a palindrome partitioning of S.ExampleGiven s = "aab" ,Return 1 since the palindrome partitioning ["AA", "B"] could be produced using1 cut. Public classSolution {/** * @params A String *@returnAn integer*/ Public intmincut (String s) {//Write your code here if(s = =NULL|| S.length () = = 0) return0; intLen =s.length (); Boolean[] Ispalindrome
Design a data structure that supports the following and the operations:void Addword (Word) bool Search (word)Search (Word) can search a literal word or a regular expression string containing only letters a-z or . . A . means it can represent any one letter.For example:Addword ("Bad") Addword ("Dad") Addword ("Mad") Search ("pad"), Falsesearch ("bad"), Truesearch (". Ad") Truesearch ("B..")-TrueNote:You may assume this all words is consist of lowercase letters a-z .Click to show hint.You should i
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.