- 博客(148)
- 资源 (5)
- 收藏
- 关注
原创 【LeetCode】292 Nim Game
题目链接Nim Game分析你和朋友共同取一堆石头,每次可以取1-3个。你先取石头,谁最终把石头取完则获胜。简单的找规律题。只要石头的个数是4的倍数则你一定会输。代码class Solution {public: bool canWinNim(int n) { if(n%4==0){ return false; }else{
2015-12-13 18:30:59
591
原创 【LeetCode】283 Move Zeroes
题目链接Move Zeroes分析把数组中的0移到数组的末尾,非0的数按照原来的先后顺序排在数组前面。简单题。只要把原来数组中非0的数按照出现的先后顺序进行标记就行了。代码 class Solution { public: void moveZeroes(vector<int>& nums) { int cnt = 0;
2015-12-13 18:29:52
438
原创 【LeetCode】278 First Bad Version
题目链接First Bad Version分析给出一个数组,如果有一个数字是badversion,则其后的数字都是badversion。要求找出第一个出现badversion的位置。按顺序查找会超时,用二分法就可以轻松过了。代码 // Forward declaration of isBadVersion API. bool isBadVersion(int version);
2015-12-13 18:28:31
425
原创 【LeetCode】14 Longest Common Prefix
题目链接Longest Common Prefix分析简单题。寻找string数组中的最长公共字符串。代码 class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size()==0){ return "
2015-12-13 16:42:01
396
原创 【LeetCode】9 Palindrome Number
题目链接String to Integer(atoi)分析输入一个int数字,判断该数字是否满足回文串的特点。 负数的话一律认为不符合代码1 我直接用vector自带的“==”来判断,这方法很便捷,但应该不是题目本意。 class Solution { public: bool isPalindrome(int x) { if(x<0
2015-12-13 16:15:45
381
原创 【LeetCode】8 String to Integer(atoi)
题目链接String to Integer(atoi)分析自己实现atoi函数的功能,将字符串转为数字。需要注意的case:前面有空格 Input: ” 010” Expected: 10 中间有字母 Input: ” -0012a42” Expected: -12超出int范围时: Input: “2147483648”
2015-12-13 15:41:33
319
原创 【LeetCode】7 Reverse Integer
题目链接Reverse Integer分析对整数进行反转输出。如果发生了溢出现象,要返回0.代码 class Solution { public: int reverse(int x) { int tmp, y=0; while(x!=0){ tmp = y*10 + x%
2015-12-13 14:27:40
348
原创 【LeetCode】6 ZigZag Conversion
题目链接ZigZag Conversion分析按行来处理,判断每行的字符在原输入字符串中的距离。 首尾行和中间行情况不同,要分开处理。几个特殊的case: (1)s为空 (2)s.size() <= numRows代码 class Solution { public: string convert(string s, int numRows) {
2015-12-13 09:47:33
336
原创 【LeetCode】299 Bulls and Cows
题目链接Bulls and Cows分析题目给出俩个字符串secret和guess。找出bull和cow的个数。 bull: 位置相同并且字符相同; cow: 字符相同但是位置不同;注意这个case的输出结果: Input: “1122” “1222” Expected: “3A0B”代码 string num2str(int num){
2015-12-11 15:11:04
353
原创 【LeetCode】303 Range Sum Query - Immutable
题目链接Range Sum Query - Immutable分析简单题。加设一个 vector<int> sum sum[i] 代表从第0个数到第i个数之和。#### **代码** class NumArray { public: vector<int> sum; NumArray(vector<int> &nums) {
2015-12-11 15:09:17
341
原创 【LeetCode】1 Two Sum
#### 题目链接 Two Sum分析给出一个数组,找出俩个数,二者之和为指定的target。注意点: (1)返回的是二者的下标; (2)数组元素有可能是负数;采用结构体来标记一个节点的值和下标,将节点按照值的大小排序,而后采用二分查找。代码struct node{ node(int _val, int _pos){ val = _val; p
2015-12-11 15:03:05
391
原创 【PAT】1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in
2015-12-05 22:03:18
1683
6
原创 【PAT】1104. Sum of Number Segments (20)
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3)
2015-12-05 22:00:46
1418
原创 【PAT】1106. Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on
2015-12-05 21:58:54
1158
原创 【PAT】1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of th
2015-12-05 21:56:54
1583
2
原创 【PAT】1034. Head of a Gang (30)
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be t
2015-11-20 11:16:43
900
原创 【POJ】2524 Ubiquitous Religions
DescriptionThere are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your
2015-11-17 09:57:41
295
原创 【POJ】1611 The Suspects
DescriptionSevere acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best s
2015-11-16 21:18:23
336
原创 【PAT】1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root
2015-11-16 20:37:12
423
原创 【PAT】1103. Integer Factorization (30)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive
2015-11-05 20:05:07
1520
原创 【PAT】1045. Favorite Color Stripe (30)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts
2015-11-05 16:04:19
447
原创 【PAT】1038. Recover the Smallest Number (30)
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-8
2015-11-05 14:42:29
424
原创 【PAT】1068. Find More Coins (30)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However,
2015-11-05 10:45:31
412
原创 【PAT】1049. Counting Ones (30)
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's i
2015-11-04 20:28:47
311
原创 【PAT】1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the hou
2015-11-04 16:56:05
340
原创 【PAT】 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a
2015-11-04 09:49:22
414
原创 【PAT】1057. Stack (30)
Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Po
2015-11-03 21:59:19
400
原创 【PAT】1091. Acute Stroke (30)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to c
2015-11-03 14:35:46
1667
原创 【PAT】1053. Path of Equal Weight (30)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R
2015-11-02 21:49:39
631
原创 【PAT】1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node's key.The right
2015-11-02 20:44:29
1773
原创 【PAT】1026. Table Tennis (30) (待改进)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the ava
2015-11-02 16:40:13
534
原创 【PAT】1080. Graduate Admission (30)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admissio
2015-11-02 14:12:37
636
2
原创 【PAT】1095. Cars on Campus (30)
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you a
2015-11-01 21:24:15
455
原创 【PAT】1087. All Roads Lead to Rome (30)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.Input Specification:E
2015-10-30 19:16:01
1203
原创 【PAT】1022. Digital Library (30)
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number a
2015-10-29 11:07:21
326
原创 【PAT】1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path b
2015-10-28 21:08:06
356
原创 【PAT】1018. Public Bike Management (30)
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the
2015-10-27 20:36:57
1046
原创 【PAT】1014. Waiting in Line (30)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:The s
2015-10-26 19:01:51
699
原创 【PAT】1086. Tree Traversals Again (25)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stac
2015-10-26 09:57:34
388
原创 【PAT】1078. Hashing (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key %
2015-10-25 17:06:54
370
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人