原创 【PAT乙級】1007 素數對猜想

素數篩法問題,給出一個數N,求不大於N的所有相差爲2且相鄰的素數對的對數: 讓我們定義d​n​​爲:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i個素數。顯然有d​1​​=1,且對於n>1有d​n​​是偶數。“素數對猜想”

原创 [PAT乙級] 1021 個位數統計

題目描述:即統計輸入大數的各字符的不爲0的數字的出現頻率 給定一個 k 位整數 N=d​k−1​​10​k−1​​+⋯+d​1​​10​1​​+d​0​​ (0≤d​i​​≤9, i=0,⋯,k−1, d​k−1​​>0),請編寫程序統

原创 [leetcode] 70. Climbing Stairs 爬樓梯問題

斐波那契額數列問題。 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2

原创 【pat甲級】1007 Maximum Subsequence Sum

題目描述: Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, 

原创 遞歸和分治問題

1. 輸出字符串的全排列: 採用遞歸的方式,交換兩個字符,然後再輸出打印: #include<bits/stdc++.h> using namespace std; void V(string s, int i, int n) {

原创 [leetcode]344. Reverse String

題目比較簡單,翻轉char[]字符數組: 問題描述: Write a function that reverses a string. The input string is given as an array of character

原创 [PAT甲級] 1094 The Largest Generation

題意大致爲一個多叉樹家譜,給出N個成員 1 - N,M個子樹,子樹分別爲父節點號,子節點數,子節點編號組成,要求給出最多成員的那一層的成員數以及層數。 題目不算複雜,開始時都設爲第一層,當每讀一行時就把子節點的level變爲父節點leve

原创 [leetcode] 287. Find the Duplicate Number (查找數組中重複的數)

找出重複的數,已知數組中重複的數只有1個,要求不能使用額外的空間。 Given an array nums containing n + 1 integers where each integer is between 1 and n 

原创 [leetcode] 58. Length of Last Word (字符串的末尾單詞的長度)

簡單題,求給出的字符串的最後一個單詞的長度,要注意的是字符串的末尾可能是空格,要跳過空格。 Given a string s consists of upper/lower-case alphabets and empty space

原创 【PAT甲級】1011 World Cup Betting

這題好像很簡單?就是求三行各行的最大值相乘再進行運算即可: With the 2010 FIFA World Cup running, football fans the world over were becoming increas

原创 動態規劃(poj):上樓梯問題的改進版

poj上機考試題目描述: D:上樓梯   描述 小S在玩一個叫上樓梯的遊戲。樓梯一共有n層臺階。 因爲腿長的限制,小S每次最多隻能上k層臺階。 小S是一個迷信的人,所以他不希望自己某一步走的步數的數字裏有"4",(比如4,14,44都含

原创 【leetcode】38. Count and Say

題目看起來非常的奇怪,點踩的人也非常的多。先看題目的描述: The count-and-say sequence is the sequence of integers with the first five terms as foll

原创 【leetcode】102. Binary Tree Level Order Traversal 層序遍歷遞歸形式(全局變量)

二叉樹層序遍歷遞歸形式的解決,一般問題都是直接打印遍歷的結果,這裏要將每一層的結果按照層進行打印,用隊列的方式可能存在一些問題。這裏採用了遞歸的方式進行解決。 Given a binary tree, return the level

原创 【leetcode】242. Valid Anagram

暴力解決問題是很簡單的,這裏主要學習了map的使用。 Given two strings s and t , write a function to determine if t is an anagram of s. Example

原创 stl中 map 的簡單使用

map<char, int>, map<string ,int>都可以定義,但是不能用map<char[] ,int>這種char型數組,主要的幾個操作是,map[a] = 1, 用 iterator->first, iterator->