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
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to N.For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers:1, 10, 11, 12, 13.The code is as follows:public class Solution {public int countdigitone (int n) { if (nOperation Result: (*medium) Leetcode 233.Number of Digit One
Given a binary tree, find the lowest common ancestor (LCA) of the Given nodes in the tree.According to the definition of the LCA in Wikipedia: "The lowest common ancestor is defined between," nodes V and W as the L Owest node in T, have both V and W as descendants (where we allow a node to be a descendant of itself). " _______3______ / ___5__ ___1__ / \ / 6 _2 0 8 / 7 4For example, the lowest common
Given an integer array of size n, find all elements this appear more than times ⌊ n/3 ⌋ . The algorithm should run in linear time and in O (1) space.Solution: Reference Programming Beauty 129 page, looking for a post water kingThe code is as follows:public class Solution {public listOperation Result: (Medium) Leetcode 229.Majority Element II
Delete DigitsGiven string A representative a positive integer which have N digits, remove any k digits of the number, The remaining digits is arranged according to the original order to become a new positive integer.Find the smallest integer after remove k digits.n k n,ExampleGiven an integer A = "178542" , k =4Return a string"12"The idea is very direct, delete the number of K, it should be noted that in order to delete from the previous to the back, the previous traversal, if the preceding numb
Title meaning: Full arrangementIdea: Actually look at this topic meaning, is not very want to use recursion, but still use recursion, non-recursive later do itPs:vector this thing can't go back, start the recursive method with vector, direct to 500ms, replace void, to 12ms1 classSolution {2 Public:3vectorint>> Permute (vectorint>nums) {4vectorint> >ans;5Permute1 (Ans,nums,0);6 returnans;7 }8 voidPermute1 (vectorint>> ans,vectorint> Nums,intbegin) {9 if(Begin==nums.size (
The link list has loops, returns True, otherwise returns falseIdeas: Two pointers, a fast and slow, can meet the ring, for the empty ringPS: Many lists of topics: You can use this idea1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: One BOOLHascycle (ListNode *head) { A if(Head==null)return false; -ListNode *p1,*P2; -P1=p2=head; the while(p2->n
The position of the target number in the increment array, if the target number is not in the array, return the position where it should be.Ideas: Binary lookup, comparison with neighboring numbers, attention to boundaries1 classSolution {2 Public:3 intSearchinsert (vectorint> Nums,inttarget) {4 intstart=0, End=nums.size ()-1;5 intflag=0;6 while(startend) {7flag= (start+end)/2;8 if(Nums[flag]==target)returnFlag;9 Else if(nums[flag]target) {Ten
Title meaning: Two fork tree first sequence traversal, the result exists in vectorProblem-Solving ideas: 1. Recursion (the problem is that it doesn't make sense to use recursion, so I'll stick to the code)2. IterationIterative implementations:1 classSolution {2 Public:3vectorint> Preordertraversal (treenode*root) {4vectorint>ans;5 if(root) {6treenode*temp;7Stacks; Use stacks, each time you print the top of the stack, and then eject the stack top8 S.push (root);9 while(!S.em
The title means: an int array with one number appearing once and the other number appearing two times to find this unique numberKnowledge Popularization: ~: Non-arithmetic, monocular operator 1 is 0,0 to 1;: With arithmetic, all 1 is 1, otherwise 0|: Or operation, all 0 is 0, otherwise 1^: XOR operation, same as 0, different 1Idea: The elements in the array are different or operation, then only 0 and the unique number, different or get the unique number1 classSolution {2 Public:3 intSinglen
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.