- 博客(16)
- 收藏
- 关注
原创 LC 67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".题目不难是一道easy题目,注意不要直接进行string 加法即可,要用一个数列记录每一位的值。这里主要是总结一下做法,先上代码。class Solution { public: ...
2018-03-23 11:11:22 113
原创 leetcode回文数相关的题
LC 9 Determine whether an integer is a palindrome. Do this without extra space.基础知识:int x是一个正数,在不考虑溢出的情况下如何获得最后一位:x % 10如何获得除了最后一位的数 x/10如何把一个数字y加到末尾:x*10+y没看答案之前没有什么特别好的想法,看了之后学会了一个构造某个数值的反向序列的方法:递归地...
2018-03-21 23:59:49 152
原创 21. Merge Two Sorted Lists 和 148. Sort List
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.Example:Input: 1->2->4, 1->3->4 Output: 1->1...
2018-03-21 20:15:59 114
原创 图
图的表示图的比较方便的表示方法有两种,第一种是matrix(比如有number of island这种),第二种是邻接矩阵的形式。如果题目中给了边的信息,最好转化成邻接矩阵。DFS void dfs(graph, visited, current){ if (visited[current]) return; visited[current] = true; ...
2018-03-18 01:54:13 132
原创 区间问题
常用函数:判断两个区间能否merge,merge之后的结果,以及如何按照start排列区间 bool canMerge(Interval &a, Interval &b) { return a.start < b.end && b.start < a.end; } Interval merge(Interval &a...
2018-03-16 23:47:07 109
原创 位运算
基础知识Test kth bit is set: num & (1 << k) != 0.Set kth bit: num |= (1 << k).Turn off kth bit: num &= ~(1 << k).Toggle the kth bit: num ^= (1 << k).To check if a number is...
2018-03-14 19:56:05 88
原创 LC 141. Linked List Cycle
LC 141. Linked List Cycle判断是否是循环列表。方法是使用双指针,一个快指针一个慢指针,快指针每次走两格,慢指针每次走一格。如果快指针先到达列表终点则return false,如果快慢指针相遇则return true这里面涉及到为什么出现环的话,快慢指针一定会相遇的这个问题。class Solution { public: bool hasCycle(ListNode...
2018-03-13 17:05:30 106
原创 LC 206 Reverse a singly linked list.
Reverse a singly linked list.Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL Input : Head of...
2018-03-13 15:56:16 142
原创 LC 153. Find Minimum in Rotated Sorted Array
LC 153. Find Minimum in Rotated Sorted ArraySuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the mini...
2018-03-13 08:58:30 112
原创 LC 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the ...
2018-03-12 23:09:48 133
原创 LC 238. Product of Array Except Self
238. Product of Array Except SelfGiven 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 ...
2018-03-12 23:00:13 115
原创 LC 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The right ...
2018-03-12 14:19:46 104
原创 LC 102 Binary Tree Level Order Traversal
基础中的基础,直接背就好了。用队列。因为需要在最后按层序打印,所以队列中记录层数。AC代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(...
2018-03-12 13:16:22 163
原创 Leetcode 155. Min Stack 题解
题目链接这道题给普通的Stack加上一个getMin()的操作,注意这个getMin()并不把min值pop出来。所以思路很简单,就是在正常的stack push的时候记录push之后的min值即可。思路就是用另外一个stack来记录当前min值。class MinStack { public: /** initialize your data structure here. */ ...
2018-03-06 16:02:44 161
原创 Leetcode 16. 3Sum Closest 题解
题目链接brute force O(n^3)先排一遍序,遍历两个数,第三个数用二分查找,复杂度O(n^2*lgn)遍历一个数,第二、三个数用双指针查找,转化为2Sum Closest问题,复杂度O(n^2)O(n^2lgn) class Solution { public: int threeSumClosest(vector<int>& nums, int targe...
2018-03-06 15:00:38 152
原创 Leetcode 11 Container With Most Water 题解
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two ...
2018-03-06 13:15:55 129
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人