![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 50
zhangmiaoran
这个作者很懒,什么都没留下…
展开
-
338. Counting Bits
想到了两种解法:(1)参照的是10进制数转换为2进制数的计算过程,代码如下:class Solution {public: vector countBits(int num) { vector counts; for(int i=0; i<=num; i++) { int count=0;原创 2016-04-06 19:40:44 · 358 阅读 · 0 评论 -
343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example, given n = 2, retur原创 2016-05-04 11:14:54 · 760 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321 当溢出时,返回0.参考代码:class Solution {public: int reverse(int x) { int result = 0; while(x){原创 2016-04-19 18:47:33 · 258 阅读 · 0 评论 -
347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of u原创 2016-05-03 16:31:18 · 1493 阅读 · 1 评论 -
leetcode中二叉树的遍历
整理一下...1. 前序遍历leetcode: 144. Binary Tree Preorder Traversal (1) 递归法/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tree原创 2016-05-13 20:15:34 · 454 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For e原创 2016-05-03 15:17:25 · 363 阅读 · 0 评论 -
204. Count Primes
除了C语言课本里方法,另一种求质数的算法: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 给出的参考代码:public int countPrimes(int n) { boolean[] isPrime = new boolean[n]; for (int i = 2; i < n; i++) { isPrime原创 2016-04-18 15:47:39 · 324 阅读 · 0 评论 -
Single Number I+II+III
Expected:linear runtime complexity, constant space complexity.(像当初的我直接用关联容器暴力解决…)Single Number IGiven an array of integers, every element appears twice except for one. Find that single one.public clas原创 2016-04-29 17:31:48 · 550 阅读 · 0 评论 -
287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 andn (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, f原创 2016-05-10 15:07:36 · 869 阅读 · 0 评论 -
334. Increasing Triplet Subsequence
题目:Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, k such that arr[i]原创 2016-04-11 19:33:56 · 425 阅读 · 0 评论 -
234. Palindrome Linked List
第一种方法,将链表中的值放到数组里,然后判断是否为回文(或者放一半进堆栈,然后判断是否为回文),然而时间复杂度为O(N),空间复杂度为O(N),没有达到O(N)/O(1)的要求。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *原创 2016-04-27 16:47:41 · 312 阅读 · 0 评论 -
110. Balanced Binary Tree
题目判断一棵二叉树是否为平衡二叉树(所有左右子树深度相差不超过1)。解法一/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(原创 2016-04-14 18:36:04 · 319 阅读 · 0 评论 -
337. House Robber III+198. House Robber
对二叉树的各种操作还不太熟....一开始是自己写的程序,然而逻辑有些问题,居然认为必须隔层访问(其实隔两层/三层也是可以的...所以重点是当前访问的结点是否被计入)。参考了他人的代码,解题思路如下:针对正在访问的结点,两种情况取较大值,(1)当前结点值计入,则结果是左孩子和右孩子不计入时两个子树的count;(2)当前结点值不计入,则结果是左子树两种情况(计入/不计入)下的max加上右子树在两原创 2016-04-06 20:45:41 · 469 阅读 · 0 评论 -
19.Remove Nth Node From End of List
好吧,参考的答案…感觉这种思路棒棒的…and以后要用java…public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head==null) return null; //定义两个指针,第一个保持指向最初的head指针 //使head指原创 2016-04-26 20:42:15 · 382 阅读 · 0 评论 -
203. Remove Linked List Elements
展现一下dummy指针的用处。(开始操作时head可能需要上拉到dummy)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */clas原创 2016-04-25 16:35:42 · 288 阅读 · 0 评论 -
N皇后问题
今天做了leetcode中两道关于N皇后的题目: 51. N-Queens 52. N-Queens II 这是回溯法的经典应用,自己不太会做…主要是研究了下别人的思路,然后自己写了写~N-Queens IIFollow up for N-Queens problem.Now, instead outputting board configurations, return the total原创 2016-05-17 17:11:57 · 1238 阅读 · 0 评论