leetcode

Learn about leetcode, we have the largest and most updated leetcode information on alibabacloud.com

Java for Leetcode 007 Reverse Integer

Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return-321Problem Solving Ideas:It is not difficult to flip a number, can be turned into a string type flip, can also be flipped, the main problem involves the boundary and overflow problems, using a long or biginteger can be solved.The topic is not difficult:The Java implementation is as follows: Public classSolution {Static Public intReverseintx) {if(x==0| | x==-2147483648)return0; BooleanIsnagetive=false; if(x) {is

Java [Leetcode 7] Reverse Integer

=Stringreverse (soriginal);8String sfinal =NewString ();9 intReversenum;Ten One if(Sreverse.charat (Sreverse.length ()-1) = = '-') { Asfinal = sreverse.substring (0, Sreverse.length ()-1); - if(Sfinal.length () >= maxlength sfinal.compareto (lowervalue) > 0) -Reversenum = 0; the Else -Reversenum =-integer.valueof (sfinal). Intvalue (); -}Else { -Sfinal =Sreverse; + if(Sfinal.length () >= maxlength sfinal.compareto (uppervalue) > 0)

Java for Leetcode 040 combination Sum II

Given A collection of candidate numbers (C) and a target number (T), find all unique combinations in c where the candidate numbers sums to T. Each number in C is used once in the combination.Note: All numbers (including target) would be positive integers. Elements in a combination (a1, a 2, ..., aK) must is in non-descending order. (ie, a1≤ a2≤ ... ≤ ak). The solution set must not contain duplicate combinations. For example, given candidate set and 10,1,2,7

Java [Leetcode 21]merge-Sorted Lists

Title Description:Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.Problem Solving Ideas:The title means to synthesize an ordered list of two ordered linked lists.Add to the new list by comparison.The code is as follows:public static ListNode mergetwolists (ListNode L1, ListNode L2) {ListNode list = new ListNode (0); ListNode tmp = list;while (L1! = NULL | | L2! = NULL) {if (L1 = = null) {Tmp.next = new L

Leetcode letter combinations of a Phone number (C,c++,java,python)

; return res; } tmp= (char*) malloc (sizeof (char) *length); Getlettercom (Res,digits,tmp,0,map,top); *returnsize=top; return res;}C + + source code (spents 3ms):Class Solution {public: vectorPython source code (spents 69ms):Class Solution: map=["", "", "abc", "Def", "Ghi", "JKL", "MnO", "PQRS", "TUV", "WXYZ"] length=0;res=[] # @par am {string} digits # @return {string[]} def lettercombinations (self, digits): Self.length=len (digits) self.res=[]

Java [Leetcode] Longest Common Prefix

Public classSolution {2 PublicString Longestcommonprefix (string[] strs) {3 intLength =Integer.max_value;4StringBuilder StringBuilder =NewStringBuilder ();5 if(Strs.length = = 0 | | strs = =NULL)6 return"";7 if(Strs.length = = 1)8 returnStrs[0];9 for(inti = 0; i ) {TenLength = (strs[i].length () strs[i].length (): length; One } A if(length = = 0) - return""; - for(intj = 0; J ) { the

Java for Leetcode 022 Generate parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Problem solving idea One:By observing the situation of n=2 and n=3 you can know that as long as the string at the beginning of the n=2, the end, ' (' Insert ' () ', attention to prevent duplication.The Java implementation is as follows:static public listTwo ways to solve problems:It can be observed that

[Leetcode] [019] Remove Nth Node from End of List (Java)

=NewListNode (-1);7Dummyhead.next =head;8 9ListNode fast =Dummyhead;TenListNode slow =Dummyhead; One A //Let fast point go n steps itself - for(inti = 0; I ) { - assert(Fast! =NULL); theFast =Fast.next; - } - - //fast and slow go together + //until fast reaches the end of list - while(Fast.next! =NULL) { +Fast =Fast.next; Aslow =Slow.next; at } - - //Now slow should pointing to the n

Java for Leetcode 025 Reverse Nodes in K-group

Given A linked list, reverse the nodes of a linked list K at a time and return its modified list.If the number of nodes is not a multiple of K then left-out nodes in the end should remain as it is.You may not alter the values in the nodes, and only nodes itself is changed.Only constant memory is allowed.For example,Given This linked list:1->2->3->4->5For k = 2, you should return:2->1->4->3->5For k = 3, you should return:3->2->1->4->5Problem Solving Ideas:Similar to the k=2 situation, in view of

Java for Leetcode 101 symmetric Tree

Given a binary tree, check whether it is a mirror of the itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / 2 2/\/3 4 4 3But the following are not: 1 / 2 2 \ 3 3Problem Solving Ideas:Overload a issymmetric to determine whether two trees are symmetrical, Java implementations are as follows: public Boolean issymmetric (TreeNode root) { if (root==null) return true; Return Issymmetric (root.left,root.right); } public

Java for Leetcode 094 Binary Tree inorder traversal

Problem Solving Ideas:Middle sequence traversal, Zuozi-root node-right subtreeThe Java implementation is as follows: Public listJava for Leetcode 094 Binary Tree inorder traversal

Java [Leetcode 2] Add Numbers

){ + if(L1 = =NULL){ Asum = L2.val +carry; atL2 =L2.next; - } - Else if(L2 = =NULL){ -sum = L1.val +carry; -L1 =L1.next; - } in Else{ -sum = l1.val + L2.val +carry; toL1 =L1.next; +L2 =L2.next; - } the * if(Sum >= 10){ $carry = SUM/10;Panax Notoginsengsum = sum% 10; - } the Else{ +Carry = 0; A } the +Tail.next =Newlistnode (sum)

Leetcode 4. Remove duplicate elements from an ordered array remove duplicates from Sorted array

Issue: Remove duplicates from Sorted Array II Difficulty: MediumFollow up for "Remove duplicates":What if duplicates is allowed at the 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].Answer:Two ways of thinking:(1) Two reference pointers, step back, and when the 3rd is equal to the first 2, skip(2) A reference pointer, which is compared with 3rd and 1th, when equal, skips; unequal assignmentIt is obvious that (1) is eas

Java for Leetcode 001 the Sum of

[i] in the graph to traverse, if found that there is such a value, and such a value is not numbers[i] corresponding to the node itself, then , the traversal ends.The Java code is as follows:1 ImportJava.util.HashMap;2 Public classSolution {3 Static Public int[] Twosum (int[] numbers,inttarget) {4 int[] a={0,0};5HashmapNewHashmap();6 for(inti=0;i){7 Map.put (Numbers[i], i);8 }9 for(inti=0;i){Ten intgap=target-Numbers[i]; One if(Map.g

Leetcode 202. Happy number Python implementation

Thought:Sum the squares of each number of bits for the input dataTo get the result, if it's 1, return to the real, or the result is recursive.When to return a fake:Return false instructions into the infinite loop.When will there be infinite loops?The result of a one-time squared sum, which has been obtained before, is infinitely cyclic.So, I save the results every time I get it, and if I find one that gets the results before, then I'm sure it's an infinite loop. return False1 classSolution:2

Java for Leetcode 019 Remove Nth Node from End of List

Given A linked list, remove the nth node from the end of the list and return its head.For example, 1->2->3->4->5 N = 2. 1->2->3->5.Note:Given n would always be valid.Try to do the in one pass.Problem Solving Ideas:Less difficult, note the following boundary conditions:Java implementations:static public ListNode Removenthfromend (listnode head, int n) {if (nJava for Leetcode 019 Remove Nth Node from End of List

Java for Leetcode 031 Next permutation

Next permutationTotal Accepted: 33595 submissions: 134095 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If Such arrangement is not a possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must is in-place, do not allocate extra memory.Problem Solving Ideas:The problem is not difficult, the key is to understand the meaning of the topic: if the nums as a number, that is, the r

"Leetcode" intersection of the Linked Lists in JAVA

Write a program to find the node at which the intersection of the singly linked lists begins.For example, the following, linked lists:A: a1→a2 c1→c2→c3 B: b1→b2→b3Begin to intersect at node C1.Or think about it, put the listnode in the stack, and then one pop out, until it's different, then the previous point is the merge point.On the internet did not find anything else to write this question, it seems I was the fi

[Leetcode] 189. Rotate Array Rotation

(nums)) Self.reverse (nums, 0, K) Self.reverse (Nums, K, Len (nums)) def reverse (self, nums, start, end): While start Lt End: Nums[start], nums[end-1] = nums[end-1], Nums[start] start + = 1 End-= 1C + +: Make a extra copy and then rotate. Time Complexity:o (n). Space complexity:o (n).Class solution {public : void rotate (int nums[], int n, int k) { if (n = = 0) | | (k C + +: Reverse The first n-k element

[Leetcode] [JavaScript] Longest Valid parentheses

Stack.push ({TenValue: ' (', OneWeight:-1 A }); -}Else{ - varTargetindex =-1; the for(varj = stack.length-1; J >= 0; j--){ - if(Stack[j].value = = = ' ('){ -Targetindex =J; - Break; + } - } + A if(Targetindex = = =-1){ at Stack.push ({ -Value: ') ', -Weight:-1 - }); -}Else{ - varMerge =NULL; in while(stack.length-targetin

Total Pages: 15 1 .... 10 11 12 13 14 15 Go to: Go

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.