
##笔试类编程题
GDRetop
数据研发专家,ETL工程师
展开
-
leetcode 412. Fizz Buzz
class Solution {public: vector fizzBuzz(int n) { vector ans; for(int i = 1; i<=n; i++){ string res=""; if(i % 3 == 0) res +="Fizz";原创 2016-11-23 19:22:43 · 514 阅读 · 0 评论 -
leetcode 88. Merge Sorted Array
class Solution {public: void merge(vector& nums1, int m, vector& nums2, int n) { int x = n+m-1; m--,n--; while(n >-1 || m > -1){ if(n < 0)nums1[x--]=nums1[m--]转载 2016-08-25 18:30:37 · 266 阅读 · 0 评论 -
网易2017内推笔试编程题合集(二)第一题 混合颜料
[编程题] 混合颜料你就是一个画家!你现在想绘制一幅画,但是你现在没有足够颜色的颜料。为了让问题简单,我们用正整数表示不同颜色的颜料。你知道这幅画需要的n种颜色的颜料,你现在可以去商店购买一些颜料,但是商店不能保证能供应所有颜色的颜料,所以你需要自己混合一些颜料。混合两种不一样的颜色A和颜色B颜料可以产生(A XOR B)这种颜色的颜料(新产生的颜料也可以用作继续混合产生新的颜色,XO原创 2016-08-19 19:07:02 · 1229 阅读 · 0 评论 -
leetcode 67. Add Binary
class Solution {public: string addBinary(string a, string b) { if(a.length() < b.length()) swap(a,b); int i = a.length()-1,j=b.length()-1; for(;j>=0;i--,j--){原创 2016-11-24 10:27:09 · 317 阅读 · 0 评论 -
leetcode 100. Same Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2016-08-15 12:26:53 · 242 阅读 · 0 评论 -
leetcode 349. Intersection of Two Arrays
class Solution {public: vector intersection(vector& nums1, vector& nums2) { sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); int a = 0,b = 0; vect原创 2016-08-15 11:22:46 · 318 阅读 · 0 评论 -
编程之美:第一章游戏之乐-饮料供货问题
书中已经提到了用背包方法解,复杂度为V*N*max(C),空间复杂度为O(v)这里想了一些其它方法以及谈谈书中提到的贪心算法方法1: 采用单调队列优化复杂度为O(V*n),空间为2*V单调队列优化方法参考背包九讲。是可以实现的。但n很大的时候,就不是很好了方法2:二进制表示法优化复杂度O(V*N*log(C)),空间为V同样参考背包九讲,对C分解成1,2,4,8原创 2017-08-17 19:20:32 · 489 阅读 · 0 评论 -
hihocoder 编程练习赛21
A集合计数#include#include#include#includeusing namespace std;int num[100007];int mod = 1000000007;#define ll long longll cal(ll a, ll b){ if(b < 0) return 0; ll ans = 1; while(b){原创 2017-08-06 14:11:18 · 408 阅读 · 0 评论 -
hihocoder [Offer收割]编程练习赛19
题目1 : 大礼堂地毯枚举#include#include#include#includeusing namespace std;char pattern[51][51];char map[101][801];int n, m,k;int work(int h,int w){ for(int i = 0;i < h; i++){ for(in原创 2017-07-23 14:49:37 · 437 阅读 · 0 评论 -
leetcode 396. Rotate Function
class Solution {public: int maxRotateFunction(vector& A) { int ans = 0,result=0; int sum = 0; for(int i = 0;i < A.size() ;i++) ans += A[i]*i, sum += A[i];原创 2016-12-18 21:18:16 · 518 阅读 · 0 评论 -
leetcode 434. Number of Segments in a String
class Solution {public: int countSegments(string s) { int ans = 0,flag = 0; for(int i = 0;i < s.length(); i++){ if(s[i] == ' '){ ans += flag;原创 2016-12-18 21:10:20 · 513 阅读 · 0 评论 -
ccf csp第八次认证
首先今天参加的考试,也不知道对不对,但是也不会改了。拿出来就是分享一下。wa了别怪我哈。嘻嘻~~~~~~第一题求相邻两数的最大差的绝对值#include#include#includeusing namespace std;#define maxn 10000int num[maxn];int main(){ int n; scanf("%d",&n); int ans原创 2016-09-11 22:54:53 · 6263 阅读 · 17 评论 -
leetcode 190. Reverse Bits
class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t x=0; for(int i = 0;i < 32 ;i++){ x<<=1; x|=n&1; n>>=1; } re原创 2016-11-24 10:58:46 · 313 阅读 · 0 评论 -
leetcode 447. Number of Boomerangs
class Solution {public: int numberOfBoomerangs(vector>& points) { unordered_map haha; int ans = 0; for(int i = 0;i < points.size(); i++){ haha.clear();原创 2016-11-24 10:50:38 · 443 阅读 · 0 评论 -
leetcode 401. Binary Watch
class Solution {public: vector readBinaryWatch(int num) { vector res[11]; vector ans[11]; vector result; if(num > 10 || num < 0) return result; for(int i =原创 2016-11-23 21:59:30 · 429 阅读 · 0 评论 -
leetcode 463. Island Perimeter
class Solution {public: int islandPerimeter(vector>& grid) { int h = grid.size(); int w = grid[0].size(); int ans = 0; int dir[4][2]={ 0,1,原创 2016-11-23 21:08:23 · 331 阅读 · 0 评论 -
leetcode 455. Assign Cookies
class Solution {public: int findContentChildren(vector& g, vector& s) { sort(g.begin(),g.end()); sort(s.begin(),s.end()); int ans = 0; int i = g.size()-1;原创 2016-11-23 20:56:44 · 323 阅读 · 0 评论 -
leetcode 136. Single Number
class Solution {public: int singleNumber(vector& nums) { int x = 0; for(int i = 0;i < nums.size(); i++) x ^= nums[i]; return x; }};原创 2016-11-23 19:53:51 · 380 阅读 · 0 评论 -
leetcode 453. Minimum Moves to Equal Array Elements
class Solution {public: int minMoves(vector& nums) { int x = nums[0]; for(int i = 0;i < nums.size() ;i++) x = min(x,nums[i]); int sum = 0; for(int i =原创 2016-11-23 19:48:00 · 376 阅读 · 0 评论 -
leetcode 389. Find the Difference
class Solution {public: char findTheDifference(string s, string t) { int x[26]={0}; for(int i = 0;i < s.length(); i++) x[s[i]-'a']++; for(int i = 0;i < t.lengt原创 2016-11-23 19:42:43 · 288 阅读 · 0 评论 -
leetcode 409. Longest Palindrome
class Solution {public: int longestPalindrome(string s) { int hehe[128]={0}; for(int i = 0;i < s.length(); i++) hehe[s[i]]++; int flag = 0; int sum = 0原创 2016-11-23 19:36:58 · 277 阅读 · 0 评论 -
leetcode 404. Sum of Left Leaves
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2016-11-23 19:32:49 · 380 阅读 · 0 评论 -
leetcode 387. First Unique Character in a String
class Solution {public: int firstUniqChar(string s) { int x[26]={0}; for(int i = s.length()-1; i >= 0; i--){ if(x[s[i]-'a'] == 0) x[s[i]-'a'] = i+1; else i原创 2016-11-23 19:29:42 · 394 阅读 · 0 评论 -
leetcode 415. Add Strings
class Solution {public: string addStrings(string num1, string num2) { string &a =num1; string &b = num2; if(a.length() < b.length()) swap(a,b); int x1原创 2016-11-23 19:17:39 · 293 阅读 · 0 评论 -
leetcode 405. Convert a Number to Hexadecimal
class Solution {public: string toHex(int num) { if(num == 0) return "0"; string ans = ""; unsigned int x = num; while(x > 0){ int f = x %原创 2016-11-23 19:08:25 · 305 阅读 · 0 评论 -
leetcode 459. Repeated Substring Pattern
class Solution {public: bool repeatedSubstringPattern(string str) { if(str.length() == 1) return false; cout<<str.length()<<endl; int *pre = new int[str.length原创 2016-11-23 18:59:48 · 403 阅读 · 0 评论 -
leetcode 437. Path Sum III
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2016-11-23 18:32:47 · 284 阅读 · 0 评论 -
leetcode 441. Arranging Coins
class Solution {public: int arrangeCoins(int n) { long long m = sqrt(1.0*n*2); if((m+1)*m/2 < n) m++; if((m+1)*m/2 > n) m--; return m; }};原创 2016-11-23 17:44:37 · 316 阅读 · 0 评论 -
438. Find All Anagrams in a String
class Solution {public: vector findAnagrams(string s, string p) { int num[26]={0}; for(int i = 0;i < p.length(); i++) num[p[i]-'a']++; int f = 0; for(i原创 2016-11-23 17:36:20 · 335 阅读 · 0 评论 -
leetcode 234. Palindrome Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool isPalin原创 2016-11-23 16:39:04 · 265 阅读 · 0 评论 -
leetcode 58. Length of Last Word
class Solution {public: int lengthOfLastWord(string s) { int be = s.length()-1,end = s.length()-1; for(;end >= 0;){ if(s[end] != ' ') break; end--;原创 2016-11-23 15:18:57 · 264 阅读 · 0 评论 -
leetcode 203. Remove Linked List Elements
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* re原创 2016-11-23 15:15:06 · 240 阅读 · 0 评论 -
leetcode 14. Longest Common Prefix
class Solution {public: string longestCommonPrefix(vector& strs) { if(strs.size() == 0) return ""; int ans = 0; for(;;ans++){ if(strs[0].size() ==原创 2016-11-23 15:07:12 · 285 阅读 · 0 评论 -
Leetcode 400. Nth Digit
class Solution {public: int findNthDigit(int n) { int digit = 1; long long t = 1; int ans = 0; while(true){ if(digit * 9 * t < n){ n =原创 2016-11-23 14:53:49 · 295 阅读 · 0 评论 -
leetcode 125. Valid Palindrome
class Solution {public: bool check(char &x){ if(x >= 'A' && x <= 'Z') x = x - 'A' + 'a'; if(x >= 'a' && x <= 'z') return true; if(x >= '0' && x <= '9') return true;原创 2016-09-07 16:49:28 · 382 阅读 · 0 评论 -
leetcode 223. Rectangle Area
class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int x1 = max(A,E),x2=min(C,G); int y2 = min(D,H),y1=max(B,F); int t1 = 0,原创 2016-08-25 19:09:46 · 360 阅读 · 0 评论 -
leetcode 205. Isomorphic Strings
class Solution {public: int haha[256]={0},hehe[256]={0}; bool isIsomorphic(string s, string t) { if(s.length() != t.length()) return false; for(int i = 0;i < s.length(); i++){原创 2016-08-25 18:53:51 · 418 阅读 · 0 评论 -
leetcode 225. Implement Stack using Queues
class Stack {public: // Push element x onto stack. queue stack; void push(int x) { stack.push(x); } // Removes the element on top of the stack. void pop() { q原创 2016-08-25 18:38:05 · 273 阅读 · 0 评论 -
leetcode 160. Intersection of Two Linked Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: int back(Lis原创 2016-08-25 18:17:49 · 281 阅读 · 0 评论 -
leetcode 290. Word Pattern
class Solution {public: string ans[26]; bool wordPattern(string pattern, string str) { int i = 0; for(int j = 0;j < str.length(); j++){ string res ="";原创 2016-08-25 18:05:26 · 401 阅读 · 0 评论