自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(300)
  • 资源 (1)
  • 收藏
  • 关注

转载 MathType公式与Word文档文字对齐的解决办法

部分MathType公式与文档文字不能很好的对齐,而是浮起来了,也就是说MathType的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题。解决办法:1、文件,页面设置,文档网络,无网格,确定2、字体,字符间距,位置,标准;格式,段落,中文版式,对齐方式,居中。这样公式和文字在水平方向上的中心就是一致的了。

2013-12-13 12:10:54 2526

转载 深入分析ConcurrentHashMap

转载自:http://www.infoq.com/cn/presentations/youdao-cloud-notes-cloud-architecture-feature术语定义术语英文解释哈希算法hash algorithm是一种将任意内容的输入转换成相同长度输出的加密方式,其输出被称为哈希值。哈希表hash t

2013-10-07 23:21:40 839

转载 Permutation Sequence

public class Solution { public String getPermutation(int n, int k) { StringBuilder sb = new StringBuilder(); for(int i=1; i<=n; i++) {sb.append(i);} StringBuil

2013-10-06 21:09:48 3275 1

原创 Best Time to Buy and Sell Stock III

很经典啊class Solution {public: int maxProfit(vector &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function int len=prices.size();

2013-10-01 00:35:29 933

原创 Best Time to Buy and Sell Stock II

一开始考虑的动态规划的想法,dp[i]=max(dp[j]+a[i]-a[j]),结果超时。其实这是没有必要的,因为如果abc三个数,当ac的时候,只需要考虑ab;如果aclass Solution {public: int maxProfit(vector &prices) { // Start typing your C/C++ solution below

2013-09-30 21:47:29 1058

原创 求最小的K个数

这样的题目写过很多,我这次要说明的是,一定要按照自己的想法来写,不要死记书上的解法,那样的话,你并没有转化为自己的东西#include #include #include #include using namespace std;vector res;void swap(vector &v,int i,int j){ int tmp=v[i]; v[i]=v[j]; v

2013-09-26 14:37:26 660

原创 Write a method that returns all subsets of a set

分析:1、每次向集合A中添加一个元素x,集合A为部分元素的所有组合  2、拷贝A,记为A1,将A1中每个元素和x进行拼接,然后将生成的元素放入A中void addAllOne(vector &to,vector &from){ for(int i=0;i<from.size();i++) to.push_back(from[i]);}void addAllTwo(vector >

2013-09-21 18:29:00 736

原创 划分

三路划分,参考了前令的博客。class Solution {public: void sortColors(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=0,mid=0,

2013-09-15 20:11:49 760

原创 Palindrome Number

思路:将元素从右向左用霍纳法则相加,得到结果。如果有溢出,r和x不会相等。class Solution {public: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function i

2013-09-15 19:16:48 606

原创 Remove Duplicates from Sorted List II

用map记录每个整数出现的次数,重建链表/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {

2013-09-15 16:40:52 736

原创 Remove Duplicates from Sorted List

这是个比较简单的题目,关键要细心/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {publ

2013-09-15 16:20:40 687

原创 Jump Game II

一开始写了一个O(n^2)的方法,大数据果断超时代码如下:class Solution {public: int jump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int dp[

2013-09-15 15:54:39 943

原创 Jump Game

dp[i]表示到达i这个位置还能最多想右走几步,则有dp[i]=max(dp[i-1]-1,A[i]);class Solution {public: bool canJump(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main()

2013-09-15 13:44:14 727

原创 N-Queens

做这道题除了很多小问题:1、v[i][col[i]]=‘Q’ 写成了str[col[i]]=‘Q’ ,殊不知这样不会修改向量中的值2、回溯的时候,col.pop_back写成了push_backclass Solution {public: vector col; vector > solveNQueens(int n) { // Start ty

2013-09-15 00:52:18 758

原创 reverse Integer

class Solution {public: int reverse(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function bool flag=true; if(x<0) flag=false;

2013-09-13 23:32:09 695

原创 Balanced Binary Tree

class Solution {public: bool isBalanced(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function bool flag=true; height(root

2013-09-13 00:29:47 706

原创 Longest Substring Without Repeating Characters

class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s=="") return 0;

2013-09-13 00:18:45 641

原创 Search in Rotated Sorted Array

class Solution {public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int left=0,right=n-1;

2013-09-12 23:14:18 566

原创 Same Tree

class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!p&&!q) return true;

2013-09-12 22:45:09 1942

原创 链表分段反转

链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现#include#include#includeusing namespace std;struct Node{ int data; Node *next;};void

2013-09-11 17:42:16 1808

原创 实习中用到的Linux命令总结

1、ps -ef  查看进程,经常通过管道和grep连用2、netstat -an 显示网络中所有连接的端口并用数字表示3、lsof -i:端口号  查看占用该端口号的进程4、scp libsoambase.so root@9.21.62.10:/root  将本机libsoambase.so文件发送到9.21.62.10机器的/root目录下5、& 表示在放在后台运行6、wh

2013-09-02 23:29:51 840

原创 strtok 分割字符串

#include#include#includeint main(){ char ch[]="I\nam\nstudent"; char *tok=NULL; tok=strtok(ch,"\n"); while(tok){ printf("%s\n",tok); tok=strtok(NULL,"\n"); } system("pause"); return 0

2013-09-02 23:00:28 816 1

原创 1-n个元素中查找缺失的一个数

题意:已知一个有序数列1-n(元素值)中缺少了一个元素,找出这个元素。1、异或可解 时间复杂度O(n)2、二分 时间复杂度O(lgn)代码如下://二分查找有序数组中缺失的一个元素,时间复杂度尽可能低 int bsearch(int a[],int left,int right){ while(left<=right){ int mid=left+(right-le

2013-09-01 16:01:55 3339 2

转载 hdu 2686

这个题目在面试的时候被问到了,首先问我的是一个人版本的,然后增加到两个人版本的。题目的含义是:一个n*n的棋盘,每个棋盘上有金矿,该金矿拿走后就没有了,问一个人从(0,0)->(n,n)然后再从(n,n)->(0,0)(这次的路径不能和上次的重叠),使得两次的和最大。这道题目我一点思路也没有,网上看了一下,据说是双线程dp,状态转移方程为:dp(k,x1,y1,x2,y2)=max

2013-09-01 00:20:36 734

原创 堆中插入元素

只需要将最后一个位置++,将插入的值放入最后一个位置中,然后向上进行调整代码如下(最大堆):int cnt=0;int heap[1000];void heapInsert(int x){ cnt++; heap[cnt]=x; int i=cnt; while(1){ if(i==1) break; int p=i>>1; if(heap[p]>=

2013-08-31 21:54:31 1420 1

原创 二叉树-最近公共祖先(LCA)

思路:递归的思想,如果当前节点的左子树和右子树各包括一个节点,则该节点就为最近公共祖先;如果当前节点等于其中的一个节点,则当前节点为最近公共祖先;如果当前节点的左子树或者右子树包括两个节点,则需要递归求该节点的左子树或者有子树。struct Node{ int data; Node* left, *right;};//count the number of p and q//

2013-08-31 00:39:20 871

原创 循环数组中查找一个数

int findRes(int a[],int l,int r,int x);int bsearch(int a[],int l,int r,int x);int findFunc(int a[],int n,int x){ if(a==NULL||n<=0) return -1; return findRes(a,0,n-1,x);}int findRes(int a[],in

2013-08-26 23:53:41 823

原创 相伴一生

string get_substring(string str){ if(str=="") return ""; int len=str.length(),from,to,max1=-INT_MAX,max2=-INT_MAX; int* one=new int[len]; int* zero=new int[len]; int* diff=new int[len]; memset(o

2013-08-18 00:12:17 662

原创 给定无序数组A,在线性时间内找到i和j,j>i,并且保证A[j]-A[i]是最大的。

void get_result(int A[],int n,int &from,int &to){ if(A==NULL||n<=0) return; int min=A[0],min_pos=0,max=-INT_MAX; for(int j=1;j<n;j++){ if(A[j]-min>max){ max=A[j]-min; from=min_pos; to=j;

2013-08-17 14:30:48 1650

原创 2.5

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node’s next pointer points t

2013-08-01 23:25:09 501

原创 2.4

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function

2013-08-01 00:03:37 494

原创 2.3

Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.EXAMPLEInput: the node ‘c’ from the linked list a->b->c->d->eResult: nothing is retu

2013-07-31 22:49:26 548

原创 2.2

Implement an algorithm to find the nth to last element of a singly linked list.分析:查找链表倒数第n个元素,这在剑指offer上有相同的习题Node* get_lastn(Node* head,int n){ if(head==NULL||n<=0) return NULL; Node* front=hea

2013-07-31 22:39:51 511

原创 2.1

Write code to remove duplicates from an unsorted linked list. FOLLOW UP. How would you solve this problem if a temporary buffer is not allowed?分析:如何允许使用额外空间,我们可以使用map来记录已经存在的节点,遇到重复的节点就删除;如果不允许使用额外空

2013-07-31 22:08:49 502

原创 1.8

Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring

2013-07-31 18:29:03 522

原创 1.6

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?分析:4个bytes告诉我们可以用整型来表示每个元素,关键是旋转

2013-07-30 14:23:07 793

原创 1.5

Write a method to replace all spaces in a string with ‘%20’.思路:耗费空间的解法是,再定义一个数组,将原数组复制到新数组中,复制的过程中进行替换,空间复杂度O(n); 优化:直接在原数组中操作,不过要从后往前,不然会造成覆盖。这让我想到了memcpy实现的过程中也可能出现覆盖需要从后往前覆盖的情况。时间复杂度:O(n)

2013-07-30 13:43:04 534

原创 1.4

Write a method to decide if two strings are anagrams or not.思路:使用一个count记录每个字符出现的次数,若出现的次数都相同,说明两个词是变位词bool isStringAnagrams(char s[],char t[]){ if(s==NULL||t==NULL||strlen(s)!=strlen(t))

2013-07-30 11:17:14 485

原创 1.3

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not

2013-07-30 10:50:58 485

原创 1.2

Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)void reverse(char* str){ if(str==NULL) return; int i=0,j=s

2013-07-29 22:46:30 509

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除