Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad ex…
Text Justification Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can i…
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring (2019年1月22日,复习) [6]ZigZag Conversion (2019年1月22日,复习) [8]String to Integer (atoi) (2019年1月22日,复习) [10]Regular Expression Matching (2019年1月22日,复习) [12]In…
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无序数组中最小/大的K个数)(2018年11月30日第一次复习) 给了一个无序数组,可能有重复数字,找到第 k 个最大的元素并且返回这个元素值. 题解:直接用直接用个堆保存数组中最大的 K 个数.时间复杂度是 O(NlogK). //时间复杂度是 O(NlogK), 用堆辅助. class Solut…
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. 题解:先按照interval的begin从小到大s…
[334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列递增.题目要求time complexity: O(N),space complexity: O(1) Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k …
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日) 给了一个乱序的数组,返回一个结果数组,数组里面每个元素是一个三元组, 三元组的和加起来为0. 题解:先固定第一个数,然后后面两个数的控制用夹逼定理,2 pointers 来解. class Solution { public: vector<vector<int>> threeSu…
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: bool isValid(string s) { set<char> st{'(', '[', '{'}; map<char, char> mp{{'(', ')'}, {'{', '}'}, {'[', ']'}}; stack<char> stk; for (auto…
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单位.问我能不能到达最后一个 index. 题解:虽然是贪心分类,我还是用dp解了.dp[i] 代表我能不能到达第 i 个位置. c…
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class Node { public: int val; vector<Node*> neighbors; Node() {} Node(int _val, vector<Node*> _neighbors) { val = _val; neighbors = _neighbors; } };…
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find First and Last Position of Element in Sorted Array [35]Search Insert Position [50]Pow(x, n) [69]Sqrt(x) [74]Search a 2D Matrix (2019年1月25日,谷歌tag复习)剑指of…
第一部分 水塘抽样 reservoir sampling 水塘抽样的原理:(应该开一篇新文章)pssss [382]Linked List Random Node (2018年11月15日,新算法) 给了一个单链表,要求等概率的返回单链表的一个结点的值. 解法:我直接随便解了,能过,但是肯定不是面试官想要的XD.先遍历一遍链表求链表长度,然后随机一个出这次是第 x 个结点,(x = rand() % length),然后再遍历到这个结点返回值. /** * Definition for sing…
[346]Moving Average from Data Stream [353]Design Snake Game [363]Max Sum of Rectangle No Larger Than K [582]Kill Process [621]Task Scheduler [622]Design Circular Queue [641]Design Circular Deque [862]Shortest Subarray with Sum at Least K…
[587]Erect the Fence [892]Surface Area of 3D Shapes…
  打家劫舍(House Robber)是LeetCode上比较典型的一个题目,涉及三道题,主要解题思想是动态规划,将三道题依次记录如下: (一)打家劫舍 题目等级:198.House Robber(Easy) 题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint…
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever…
Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the…
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &…
[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 直译:给你一个字符串, 判定它是否是回文(仅仅统计字母.数字.其它字符请忽略). For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car"…
C/C++数字范围(32位系统) ~ // 1 字节 char // 1 字节 ~ // 2 字节 - // 4 字节 unsigned: - // 4 字节 size_t: ~ // 4 字节 - // 4 字节,Linux64位下的long占 8 字节!!! unsigned - // 4 字节 - // 8 字节 unsigned - // 8 字节 __int64: - - // 8 字节 unsigned __int64: - // 8 字节 ~ 3.40282e+038 // 4 字…
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为…
1. Description Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. No…
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1: Given words = ["bat", "tab", &qu…
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5 you should return [0,1,1,2,1,2]. 2. Answ…
1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move you…
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p…
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 题目关键点:k可能比链表长度还大, 需要获取链表长度len, 用 k % len 获取具体需要移动的数字. class Solution…
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2…
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快…
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:太简单! bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL &…