头像

Sei


访客:4828

离线:6个月前


活动打卡代码 AcWing 38. Count and Say

Sei
8个月前
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        prev_person = '1'
        for i in range(1,n):
            next_person, num, count = '', prev_person[0], 1
            for j in range(1,len(prev_person)):
                if prev_person[j] == num:count += 1
                else:
                    next_person += str(count) + num
                    num = prev_person[j]
                    count = 1
            next_person += str(count) + num
            prev_person = next_person
        return prev_person



Sei
8个月前
class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length==0) return 0;
        int k = 1;
        for(int j =1;j<nums.length;j++){
            if(nums[j]!=nums[j-1]){
                nums[k++] = nums[j];
            }
        }
        return k;
    }
}


活动打卡代码 AcWing 88. Merge Sorted Array

Sei
8个月前
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = m-1, j=n-1,k = m+n-1;
        while(i>=0 && j>=0){
            if(nums1[i]>nums2[j]){nums1[k--] = nums1[i--];}
            else{nums1[k--] = nums2[j--];}
        }
        while(j>=0) nums1[k--] = nums2[j--];
    }
}



Sei
8个月前
class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        res = ' '
        s = s.split()
        s.reverse()
        return res.join(s)



Sei
8个月前

java 代码

import java.util.*;

public class Main{
    public static void main(String[] args){
        long a, b, p;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i =0;i<n;i++){
            a = sc.nextLong();
            b = sc.nextLong();
            p = sc.nextLong();
            System.out.println(fastPow(a, b, p));
        }
    }
    public static long fastPow(long a, long b, long p){
        long res = 1%p, base = a;

        while(b!=0){
            if((b&1)==1){res=(res*base)%p;}
            base = base*base%p;
            b>>=1;
        }
        return res;
    }
}



Sei
8个月前

py 代码

N, V, M = tuple(map(int, raw_input().split()))
goods = [0]*N
for i in range(N):
    tmp = raw_input()
    goods[i] = tuple(map(int, tmp.split()))

dp = [[0 for j in range(M+1)]for i in range(V+1)]
for i in range(N):
    for j in range(V, goods[i][0]-1, -1):
        for k in range(M, goods[i][1]-1, -1):
            dp[j][k] = max(dp[j][k], dp[j-goods[i][0]][k-goods[i][1]]+goods[i][2])
print dp[V][M]



Sei
8个月前

python 代码

v = []
w = []
N, V = tuple(map(int, raw_input().split()))
for i in range(N):
    tmp = raw_input()
    wi, vi = tuple(map(int, tmp.split()))
    v.append(vi)
    w.append(wi)
dp = [0]*(V+1)
for i in range(N):
    for j in range(w[i], V+1):
        dp[j] = max(dp[j], dp[j-w[i]]+v[i])

print dp[-1]



Sei
8个月前

python 代码

N, V = tuple(map(int, raw_input().split()))
v = [0]*N
w = [0]*N
for i in range(N):
    tmp = raw_input()
    wi, vi = tuple(map(int, tmp.split()))
    v[i] = vi
    w[i] = wi
dp = [0]*(V+1)
for i in range(N):
    for j in range(V, w[i]-1, -1):
        dp[j] = max(dp[j], dp[j-w[i]]+v[i])
print dp[V]



Sei
8个月前

python 代码

m, n = tuple(map(int, raw_input().split()))
nums = map(int, raw_input().split())

def heapSort(nums, m):

    def heapify(nums, i, n):
        if i<n:
            l, r, m = 2*i+1, 2*i+2, i
            if l<n and nums[l]>nums[m]:
                m = l
            if r<n and nums[r]>nums[m]:
                m = r
            if m!=i:
                nums[i], nums[m] = nums[m], nums[i]
                heapify(nums, m, n)


    parent = (m-2)/2
    for i in range(parent, -1, -1):
        heapify(nums, i, m)
    for i in range(m-1, -1, -1):
        nums[i], nums[0] = nums[0], nums[i]
        heapify(nums, 0, i)

heapSort(nums, m)
print ' '.join(map(str, nums[:n]))



Sei
8个月前

python 代码

n = int(raw_input())
nums = map(int, raw_input().split())

def quick(nums, n):

    def partition(nums, l, r):
        p = nums[l]
        while l<r:
            while l<r and p<=nums[r]:
                r-=1
            nums[l] = nums[r]
            while l<r and p>=nums[l]:
                l+=1
            nums[r] = nums[l]
        nums[l] = p
        return l


    def quickHelper(nums, l, r):
        if l<r:
            p = partition(nums, l, r)
            quickHelper(nums, l, p)
            quickHelper(nums, p+1, r)
    quickHelper(nums, 0, n-1)
quick(nums, n)
print ' '.join(map(str, nums))