Given a string S and a string t, count the number of distinct subsequences of T in s.A subsequence of a string is a new string which was formed from the original string by deleting some (can be none) of the C Haracters without disturbing the relative positions of the remaining characters. (ie, is a subsequence of and is not "ACE" "ABCDE" "AEC" ).Here are an example:S = "rabbbit" , T ="rabbit"Return 3 .Problem Solving Ideas:DP problem, understand the recursive formula can, refer to distinct [emai
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree was defined as a binary tree in which the depth of the Every node never differ by more than 1.Problem Solving Ideas:Recursive, Java implementation is as follows: public Boolean isbalanced (TreeNode root) { if (root==null) return true; if (Math.Abs (maxDepth (root.left)-maxdepth (root.right)) >1) return false; Re
You are given, linked lists representing, and non-negative numbers. The digits is stored in reverse order and all of their nodes contain a single digit. ADD the numbers and return it as a linked list.Input: (2, 4, 3) + (5, 6, 4)Output: 7, 0, 8Problem Solving Ideas:Defines three listnode L1, L2,result, where result is the output of the return statement, L1, L2 is an incoming parameter.Assign the L1 to result, execute result.val+=l2.val, and then L1 as the pointer level down until L2.NEXT is null.
(inti=asize-1;i>=0;i--) { AResult.add (Addnumber +Result.get (i)); at } - returnresult; - } -}Then I saw the simpler way. Gray code, binary transcoding method is the binary pre-complement 0, and then two-bit XOR. GI = bi ^ bi+1 such as binary 0101, before adding 0 after 00101,Then the right-to-left 22-bit XOR 0111 (the difference is 1, the same as 0), get Gray code 0111, in fact, is the binary x Do (x>>1) ^x operation.1 Public classSolution {
an empty openingextension : What should I do when nums contains repeating characters ?1, the beginning is disorderly order, so need to nums sorting to determine whether to repeat.2. In the loop of backtracking method, the first line is added to repeat judgment, if the current element (not the first) and the previous element, then skip this element does not use recursion.1 PublicListint[] nums) {2ListNewArraylist();3 Arrays.sort (nums);4Backtrack (list,NewArraylist);5 returnlist;6 }7 8 Priv
Pascal ' s Triangle:Given numrows, generate the first numrows of Pascal ' s triangle.For example, given numrows = 5,Return[ 1], [ 1,2,1], [ 1,3,3,1], [ 1,4,6,4,1]The number of rows is known to generate the Pascal Triangle. In fact, as long as there is layer I, then you can generate the first i+1 layer. Each newly generated layer is added to the final collection.1 PublicListintnumrows) {2listNewArraylist();3 4 for(inti=0;i) {5listNewArraylist();6 for(i
than 0, then directly add, otherwise take nums[i] is the largest)"Notice is not to say that dp[i] is the maximum value that a subsequence can achieve in a sequence ending with nums[i]"So the largest value in the dp[] is the largest one required.Optimization:As long as you know the maximum value of DP, you do not need to create a DP, just use a variable mem to record the current, and then see if it is larger than Max:1 Public intMaxsubarray (int[] nums) {2 intmax = Nums[0];3
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
Slow.next2. Dummy head application: Dummy Head is we create a new node, and then let the original head node to the back of the new node. I think using the dummy head with three more obvious benefits.One is to minimize the changes to the original linked list, if the head = Head.next Such a method, head will move, so change the original situation, not very good.Two benefits are convenient to return, directly return to Dummy.next can be.Three is not the right to do special treatment, if the deleti
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
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
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
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 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.You may assume this each input would has exactly one solution.Input:numbers={2, 7, one, A, target=9Output:index1=1, index2=2Test instructions: Give the unordered array A to find the sum of
First on the topic:Given A string containing just the characters‘(‘,‘)‘,‘{‘,‘}‘,‘[‘and‘]‘, determine if the input string is valid.the brackets must close in the correct order," () " and" () []{} " are all valid But" (] " and" ([)] " are not. Write the answer yourself, test it throughpublic class Solution {public Boolean isValid (String s) {linkedlistfor (char C:s.tochararray ()){if (!stack.isempty ()){if (Stack.peek () ==40c==41| | Stack.peek () ==91c==93| | Stack.peek () ==123c==125){Stack.pop
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.