随笔分类 -  C++

摘要:先解释一下,为什么从09年开始就一直使用ubuntu10.10,大三的时候开始接触虚拟机VmWare使用Ubuntu。那时不懂什么是LTS,在这个虚拟机上安装了vim,g++,jdk,mysql,xlamp,python2.7,curl,go,adobe,ibus,chrome,firefox,qq,msn,openfetion,open office,msn等所以就不方便升级到12.04。P.S.我不喜欢12.04的新风格。进入正题搜索“ubuntu 升级到gcc4.7”sudo add-apt-repository ppa:ubuntu-toolchain-r/testsudo apt-g 阅读全文
posted @ 2012-12-01 23:08 junfeng_feng 阅读(1272) 评论(0) 推荐(0)
摘要:#include<iostream>#include<cstdio>#include<cstdlib>using namespace std;class Node {public: Node* next; Node* rand; int value;};Node* copy_list_with_rand_ptr(Node* list) { if(list == NULL) { return NULL; } /* * 参考:http://hi.baidu.com/gkqofoydngbjqtq/item/6345d6104b7a8d06e... 阅读全文
posted @ 2012-11-24 16:23 junfeng_feng 阅读(330) 评论(0) 推荐(0)
摘要:如 1,2,3,...10出现11个数字,1,2,3..11出现了13个数字。现在知道出现d个数字,求n,如果d非法,输出impossible。算法的思想是:计算n = 9, 99, 999...这些长度为1,2,3...的各个数的出现的数字个数,反推d。int calc_from_number_of_digits(int d) { if(d < 9) return d; int n = 0; int last_n = 0; int len = 1; while(d > n) { last_n = n; ... 阅读全文
posted @ 2012-11-11 23:36 junfeng_feng 阅读(249) 评论(0) 推荐(0)
摘要:二分查找查找key最左和最右的位置利用这个特点,可以在O(log n)时间 在排序的数组中,统计一个数出现的次数。如[1,2,2,3],2出现了两次//most_left_or_right: true 表示最左, false表示最右 int binary_search_most_left_or_right(int* array, int size, int key, bool most_left_or_right = true) { int low = 0; int high = size - 1; while(low <= high) { int mi... 阅读全文
posted @ 2012-11-11 23:28 junfeng_feng 阅读(150) 评论(0) 推荐(0)
摘要:class Solution { public: vector<int> plusOne(vector<int> &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> result; int carray_bit = 0; digits[digits.size()-1] += 1; for(int i=digits.size()-1;i>=0;i... 阅读全文
posted @ 2012-10-09 21:31 junfeng_feng 阅读(169) 评论(0) 推荐(0)
摘要://需要注意细节class Solution { public: string longestCommonPrefix(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function //sort(strs.begin(),strs.end()); string result(""); if (strs.size()==0) { return r... 阅读全文
posted @ 2012-10-09 21:20 junfeng_feng 阅读(184) 评论(0) 推荐(0)
摘要:使用统计学的方法:O(n)分治的方法,比较复杂class Solution { public: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int maxendinghere=A[0]; int max = maxendinghere; for(int i=1;i<n;i++) { if (maxendin... 阅读全文
posted @ 2012-10-09 20:53 junfeng_feng 阅读(199) 评论(0) 推荐(0)
摘要:http://www.leetcode.com/onlinejudgeclass Solution { public: vector<string> anagrams(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function multimap<string,string> map; for(int i=0;i<strs.size();i++) { if(s... 阅读全文
posted @ 2012-10-09 17:12 junfeng_feng 阅读(162) 评论(0) 推荐(0)
摘要:直接使用加法/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below ... 阅读全文
posted @ 2012-10-09 16:10 junfeng_feng 阅读(163) 评论(0) 推荐(0)
摘要:LeetCode interview Questions:Add binay1.int之类的数可以使用small2.直接在字符串使用二进制的加法http://www.leetcode.com/onlinejudge 需要FQclass Solution { public: int string_to_int(string str) { int result = 0; int level = 1; for(int i = str.length() - 1; i >=0 ; i--) { if (str[i] == '... 阅读全文
posted @ 2012-10-09 15:52 junfeng_feng 阅读(159) 评论(0) 推荐(0)
摘要:#include<iostream> using namespace std; int main() { uint32_t size; cin >> size; int arr[size]; printf("size address::%x\n", &size); printf("array address::%x\n", &arr[0]); return 0; }这段代码,可以编译通过。可以在栈上开辟一个运行时才知道大小的数组。(参考:http://ericwang.github.com/c_cpp/2010/0 阅读全文
posted @ 2012-08-27 19:27 junfeng_feng 阅读(661) 评论(0) 推荐(0)
摘要:服务器:/* *run command: * g++ server.cpp -o server && ./server */ #ifndef SERVER #define SERVER #include<arpa/inet.h> #include<assert.h> #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<errno.h> #include<assert.h> #include<string.h&g 阅读全文
posted @ 2012-05-29 20:04 junfeng_feng 阅读(576) 评论(0) 推荐(0)
摘要://字符串同素:包含相同的char,以及char出现的次数#include <iostream> #include <stdio.h> #include <map> #include <assert.h> #include <string.h> #include "boost/smart_ptr.hpp" using namespace std;const int CHAR_NUMBER = 256;//答题的时候,好像写成255了 bool get(char* str,int* arr) { if( str == 阅读全文
posted @ 2012-05-29 16:54 junfeng_feng 阅读(170) 评论(0) 推荐(0)
摘要:写下书单,感叹自己大学前三年的孤陋,惋惜没有好好利用时间看看技术书籍,只会看课本学习老师教过的内容。很是惭愧,在下面的书单之前我没有额外看过除课本以外的任何技术书籍。在此特别感谢zcj,是他让我看到了我和他的差距,看到了他书桌上的计算机经典的书籍,我一下无地自容。下面所列均为计算机经典,希望在大四这一年看下这些书为时不晚!1.《C++ 编程思想》作为我的第一本课外书籍,不得不说我选对了,这个被侯捷先生归类为“C++四库全书”,看过这本书之后,我才知道自己之前以为掌握C++是多么幼稚的观点,也就是我是只“井底之蛙”。2.《Java编程思想》虽然作为我第二本买来的经典书籍,可以直到研究生之前的暑假 阅读全文
posted @ 2011-12-29 20:32 junfeng_feng 阅读(330) 评论(0) 推荐(0)
摘要:/* * mergesort_unrecursive.cpp * * Created on: Dec 14, 2011 * Author: junfeng * //从分治策略的机制入手,容易消除归并排序算法中的递归,事实上, //算法Mergesort的递归过程只是将待排序列 集合一分为二,直到待排序列集合 //只剩下一个元素为止,然后不断合并两个排好序的数组段。按此机制,可以首先 //将数组a中相邻元素两两配对。用合并算法将它们排序,构成n/2组长度为2的排好 //序的子数组段,然后再将它们 排序成长度为4的排好序的子数组段,如此继续下去, //直... 阅读全文
posted @ 2011-12-14 23:11 junfeng_feng 阅读(468) 评论(0) 推荐(0)
摘要:#include<stdio.h> #include<string> #include<stdlib.h> using std::string; string BigINTtoBinary(string n) { string result=""; string temp="temp"; while(temp.length()>0) { temp=""; int i=0; char ch; while(i<n.length()) { ch=n[i]-'0'; if(ch& 阅读全文
posted @ 2011-12-09 17:49 junfeng_feng 阅读(1240) 评论(0) 推荐(0)