随笔分类 -  LeetCode

为下半年找工作做准备哈
24. Swap Nodes in Pairs
摘要:Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. 交换两个节 阅读全文

posted @ 2016-06-01 21:06 不小的文 阅读(122) 评论(0) 推荐(0)

19. Remove Nth Node From End of List
摘要:Given a linked list, remove the nth node from the end of list and return its head. For example, 阅读全文

posted @ 2016-05-29 21:55 不小的文 阅读(161) 评论(0) 推荐(0)

21、Merge Two Sorted Lists
摘要:题目很简单,注意开辟新的链表指针时,需要用malloc分配空间,t=(ListNode*)malloc(sizeof(ListNode)); 阅读全文

posted @ 2016-05-28 15:58 不小的文 阅读(166) 评论(0) 推荐(0)

263. Ugly Number
摘要:如果一个数中只包含因子2,3,5,则为ugly number,如6、8,而14中包含7,所以不是,一般认为1是ugly number。 思路: 1、如果小于1,return false; 2、如果大于1,如果不能被2、3、5中的任何一个整除,则return false,否则,能被哪个整除就除以哪个。 阅读全文

posted @ 2016-05-28 14:33 不小的文 阅读(125) 评论(0) 推荐(0)

206. Reverse Linked List
摘要:反转一个链表 思路: 1.空或者单元素链表直接返回 2.两个元素以上 设置一个新的表头t1; 然后遍历链表,每遍历一个元素,都用插入到t1中; t2用来存储断开后的链表。 注意这里全是地址操作,比如t2=head,那么head发生变化,t2也会变。 代码: if(head==NULL||head-> 阅读全文

posted @ 2016-05-28 14:12 不小的文 阅读(105) 评论(0) 推荐(0)

171. Excel Sheet Column Number
摘要:A=1,B=2,...,AA=27 思路: 假设字符串为s A-Z代表1-26,就是26进制, 再把A-Z的ACS码-64,就是1-26,然后就是正常的数值运算 26*(s[i]-64)+s[i+1]-64; 阅读全文

posted @ 2016-05-26 00:02 不小的文 阅读(114) 评论(0) 推荐(0)

100. Same Tree
摘要:判断两个数是否相等。 子问题:两个节点是否相等 两个节点为空,则相等,此时没有左右节点。 不全为空或者值不相等,则不相等。 左节点相等&&右节点相等。 阅读全文

posted @ 2016-05-24 17:55 不小的文 阅读(75) 评论(0) 推荐(0)

283. Move Zeroes
摘要:把数组中的0都移到末尾。 void moveZeros(vector<int> nums) { int n=nums.size(); int j=0; for(int i=0;i<n;i++) { if(nums[i]!=0) { nums[j]=0; j++ } } while(j<n) { nu 阅读全文

posted @ 2016-05-24 17:19 不小的文 阅读(107) 评论(0) 推荐(0)

226. Invert Binary Tree
摘要:Invert a binary tree. to 阅读全文

posted @ 2016-05-24 16:44 不小的文 阅读(76) 评论(0) 推荐(0)

104. Maximum Depth of Binary Tree
摘要:找二叉树最长的路径。 class solution public: int maxDepth(TreeNode* root) { if(root==NULL) return 0; else return 1+max(maxDepth(root->left),maxDepth(root->right) 阅读全文

posted @ 2016-05-24 16:40 不小的文 阅读(80) 评论(0) 推荐(0)

258.Add Digits
摘要:给定正整数,如12,各位相加,直到只剩一个数字,结果为3。 class solution public: int addDigits(int num) { if(num<10) return num; else { while(num>9) { int sum = 0; sum = sum + nu 阅读全文

posted @ 2016-05-24 16:09 不小的文 阅读(85) 评论(0) 推荐(0)

292.Nim Game
摘要:一堆石头,轮流拿走1-3个,拿到最后一个石头的赢。每次游戏我先开始,给定石头个数N,判断是否我是否能赢。 class Solution {public: bool canWinNim(int n) { return N%4==0? false:true; }}; 思路:当只有4个石头的时候,谁先拿谁 阅读全文

posted @ 2016-05-24 15:51 不小的文 阅读(120) 评论(0) 推荐(0)

172. Factorial Trailing Zeroes
摘要:Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 计算n的阶乘后面有多少0 思路:就是看能被多少个 阅读全文

posted @ 2016-04-29 15:08 不小的文 阅读(102) 评论(0) 推荐(0)

【LeetCode 110】Merge Two Sorted Lists
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 将两个有序链表合 阅读全文

posted @ 2016-03-18 14:53 不小的文 阅读(112) 评论(0) 推荐(0)

导航