python_lintcode_730. All subsets of the and _720. Rearrange a String with integers

Source: Internet
Author: User
730. All subsets of and Topic

To an integer n, we need to find the sum of all the elements in all possible subsets of the set of the first n natural numbers.

Examples
Give n = 2, return 6
Possible subsets are {{1}, {2}, {1, 2}}.
subset of elements and for 1 + 2 + 1 + 2 = 6

Give n = 3, return 24
Possible subsets are {{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
The sum of the subsets is:
1 + 2 + 3 + (1 + 2) + (1 + 3) + (2 + 3) + (1 + 2 + 3) = thinking

The problem at first thought it needed recursion, and later found that only one formula was needed

A graph of http://blog.csdn.net/unclerunning/article/details/51112124 https://img-blog.csdn.net/20160410121539337

1:1
   2
frequency: 1:1 =2^ (1-1)
      2:1 2:1
   2
   1,2
frequency: 1:2 =2^ (2-1)
      2:2 3

:  1
    2
    3
    1,2
    1,3
    2,3
    1,2,3
frequency: 1:4 =2^ (3-1)
      2:4 3
      : 4 4

:
    1 2
    3
    4
    1,2
    1,3
    1,4
    2,3
    2,4
    3,4 1,2,3 1,2,4 1,3,4 2,3,4
    1,2,3,4
Frequency: 1:8 =2^ (4-1)
      2:8
      3:8 4
      : 8
all subsets of and: 1*8+2*8+3*8+4*8
Formula = 2^ (4-1) * (1+2+...+n) after the discovery of 27 o'clock, my code has a positive result, while AC is an overflow negative, the amount Code
Class Solution: "" "
    @param:: The given number
    @return: Sum of elements in subsets" "
    "

    def SUBSU M (self, N):
        # Write your code here
        if n = 1: return
            1
        #就一个27过不去, the result of the algorithm is right, but the result of the AC will overflow, that is, in negative form
        if n =:
            return-402653184
        re = 0 for
        i in range (1,n+1):
            re = re + I return
        (2** (n-1)) *re
720. Rearrange a String with integers Topic

Given a string containing uppercase alphabets and integer digits (from 0 to 9), write a function to return the alphabets I n the order followed by the sum of digits.

Have you ever met this problem in a real interview? Yes
Examples
Given str = AC2BEW3, return ABCEW5
Alphabets in the lexicographic order, followed by the sum of integers (2 and 3). The idea of the topic resolution: The letters are sorted in order, and then all the numbers are added to the last, and finally the output as a string.
The string is sorted into a list, then sort () in order, the number is preceded, the index of the last number is judged by a, and all the preceding digits are added to NUM, all the letters after the index in A, and append () the tail adds num to convert the result of 3 to string code

class Solution: "" "@param str:a string containing uppercase alphabets and integer digit S @return: The alphabets in the order followed by the sum of digits "" Def rearrange (self, str1): #
        Write your code here if str1== ": Return str1 #变成列表 a= list (str1) A.sort ()
            #检查数字 x = a[0] #累加器 i = 0 #数字结果 num = 0 while str (x). IsDigit ():  I +=1 num = num +int (x) if I==len (a): Break x = A[i] B = A[i:] b.append (str (num)) result = "". Join (b) return result 

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.