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
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
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
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
=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
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
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
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
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
[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
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
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
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
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
(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
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.