ADD Numbers
Total accepted:160702
Total submissions:664770
Difficulty:medium
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, 8The idea of the subject is very simple, the structure of two lists are from low to high order, after-tax requirements to return the linked list is also such a
The following is the time to take time to brush the first 5 questions, are the solution of their own, perhaps not the optimal solution, just finishing, convenient for future optimization and promotion1. The Sum of the sums:class Solution: # @return A tuple, (index1, index2) def twosum (self, num, target): = {} for in xrange (len (num)): if dict.get ( Target-num[i], none) = = None := I else: return (Dict[target-num[i]], i)
Problem descriptionMerge 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.AlgorithmCode One1 PublicListNode mergetwolists (listnode l1,listnode L2) {2 if(l1==NULL)3 returnL2;4 if(l2==NULL)5 returnL1;6 ListNode l3,p;7 if(l1.vall2.val) {8l3=L1;9p=L1;Tenl1=L1.next; One}Else{ Al3
The first part of the problem is the operation of a list of cases, this article mainly on a number of linked list of some problems(1) Merging two lists that have been sorted //l1, L2 Two pointers constantly moving back ListNode*mergesortedlists (ListNode *l1, ListNode *L2) {ListNode Newhead (0), *pos = Newhead; while(L1 | |L2) { if(!L1)return(Pos->nex
I have written two versions for your reference:
Recursive version
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { return addTwoNumbers(l1, l2, 0); } ListNode *addTwoNumbers(ListNode *l1, ListNode *l2, int carry) { if(l1 == NULL) return addTwoNumbers(l2, carry); if(
as the data inside. Print () will get the type that belongs to the element, not the progenitor.tu1= (1)Print (TU1) Gets the elementtu1= (1,)Tu2= (' Alex ',)Tu3= ("the")lists, tuples, strings convert each otherLists and tuples converted to strings must depend on the Join functionLists: Lists and lists can be addedL1= "the"L2= "' Alex ', ' Wusir '"L3=l1+l3Print ()When you loop a list, if you delete some of the elements in the list,Then all elements beh
Original title address: https://oj.leetcode.com/problems/add-two-numbers/Test instructionsYou 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, 8The idea of solving problems: the basic operation of the linked list. The methods is provided. Method 1 is much slimmer#Definition for singly-linked list.#class ListNo
# Include
The line_in_line () function can also be written as follows:
Int line_in_line (line L1, line l2) // intersection of line segments (including intersections) {// return (max (l1.u. x, l1.v. x)> = min (l2.u. x, l2.v. x) MAX (l2.u. x, l2.v. x)> = min (l1.u. x, l1.v.
The built-in set is easier to remember.L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']L2 = List (SET (L1 ))Print L2
There is also a speed difference that is said to be faster and never tested.L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']L2 = {}. fromkeys (L1). Keys ()Print L2
Both of them have a disadvantage. Sorting changes aft
to determine whether the list is empty. 2.8 Pop_back and Pop_front (): through Delete the last element, delete the first element by Pop_front (), the sequence must not be empty, and if the list is empty, calling Pop_back () and Pop_front () will cause the program to collapse. 2.9 Assign (): similar to the operation in the vector, there are two cases, the first is: L1.assign (n,val) changes the L1 element to n T (Val). The second case is: L1.assign (L2
two major features of the collection:First, to HeavySecond, the relationship test1, to the effect of weight>>> L1 = ["Zhang San", "John Doe", "Harry", "Zhang San"]>>> print (Type (L1))//view type>>> L1 = set (L1)//set the list into a collection>>> Print (L1){' John Doe ', ' Zhang San ', ' Harry '}>>> print (Type (L1))2, take the intersection of two sets>>> L1 = set ([1,2,3,4,5])>>> L2 = Set ([1,6,7,8,9])>>> Print (L1.intersection (
such as indexing, sharding, merging, etc.Mapping: Support for operations such as key indexingVariable types can be changed in situ, immutable types cannot be changed in situ, only one copy can be made.ListMain attributes: An ordered set of arbitrary objects, variable length, heterogeneous, arbitrary nesting, a variable type of sequence, an array of objects applied by offset accessCommon list constants and actions
Operation
Explain
L1 = []
An empty list
First, ListRole: Store personnel information, multiple names, etc.Definitions: [] separated by commas, by index, holding various data types, each position represents an element. List () can also be defined.The following are list-related commands: ## #创建listL1 = []#define an empty listL2 = ['a','b','C', [+]#the value is indexed in the way 0-5L3 = [1,2,3,['a','b']]#Nested listsL4 = List ()#define an empty list## #查询Print(L2[0])#finding the No. 0
Problem-Solving ideas: 1, create a prehead, so convenient to handle the first operation, 2, plus a carry for holding the carry number, 3, each bit of the operation, including L1.val + L2.val + carry 4, each operation results, through division and redundancy operation, get carry and when Digits in the front digits
# Definition for singly-linked list.
# class ListNode:
# def __init__ (self, x):
# self.val = x
# Self.next = None
cl
illustrate that Set1 is a subset of Set2. 6 7 Print (set2 > Set1) 8 print (Set2.issuperset (Set1)) # These two are the same, both of which illustrate that Set2 is a set1 superset. The set is mutable, the element is immutable, and the Frozenset becomes a set invariant1 s = frozenset ('Barry')2print(S,type (s) ) 3 for inch S: 4 Print (i)Depth copy1 #赋值运算 2 l1 = [3] L2 = L14 l1.append ('a' )5print("{}\t\t{}". Format (L1,
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.