基础算法记录
LALANDLI
勿在浮沙筑高台
展开
-
判断点是否位于多边形内部
判断点是否位于多边形内部转载 2016-07-01 21:49:42 · 421 阅读 · 0 评论 -
LeetCode_455. Assign Cookies
455. Assign Cookies Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the m原创 2017-02-01 17:30:44 · 271 阅读 · 0 评论 -
LeetCode_evaluate-reverse-polish-notation
链接:https://www.nowcoder.com/practice/22f9d7dd89374b6c8289e44237c70447?tpId=46&tqId=29031&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking 来源:牛客网题目描述: Evaluate the value of an arithmetic原创 2017-02-23 23:22:39 · 412 阅读 · 0 评论 -
LeetCode_453. Minimum Moves to Equal Array Elements
453. Minimum Moves to Equal Array Elements Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 ele原创 2017-02-01 18:07:09 · 256 阅读 · 0 评论 -
LeetCode_383. Ransom Note
383. Ransom Note: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from原创 2017-02-03 19:10:58 · 297 阅读 · 0 评论 -
LeetCode_349. Intersection of Two Arrays
349. Intersection of Two Arrays Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note: 1. Each element in the result m原创 2017-02-03 20:44:50 · 273 阅读 · 0 评论 -
LeetCode_387. First Unique Character in a String
387. First Unique Character in a String Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "lovelee原创 2017-02-03 22:02:03 · 221 阅读 · 0 评论 -
LeetCode_minimum-depth-of-binary-tree
minimum-depth-of-binary-tree链接:https://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724?tpId=46&tqId=29030&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking 来源:牛客网题目描述: Given a原创 2017-02-24 23:34:40 · 384 阅读 · 0 评论 -
剑指OFFER_求数组中逆序对的个数
题目描述:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007 输入描述:题目保证输入的数组中没有的相同的数字 数据范围: 对于%50的数据,size<=10^4 对于%75的数据,size<=10^5 对于%100的数原创 2017-03-19 12:16:18 · 521 阅读 · 0 评论 -
网易编程题_字符串编码
给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。 输入描述: 每个测试输入包含1个测试用例 每个测试用例输入只有一行字符串,字符串只包括大写英文字母,长度不超过10000。输出描述: 输出编码后的字符串输入例子: AAAABCCDAA输出例子: 4A1B2C1D2A#in转载 2017-03-12 14:41:26 · 971 阅读 · 0 评论 -
蓄水池采样算法(Reservoir Sampling)
蓄水池采样算法:问题描述:采样问题经常会被遇到,比如:从 100000 份调查报告中抽取 1000 份进行统计。从一本很厚的电话簿中抽取 1000 人进行姓氏统计。从 Google 搜索 “Ken Thompson”,从中抽取 100 个结果查看哪些是今年的。这些都是很基本的采用问题。既然说到采样问题,最重要的就是做到公平,也就是保证每个元素被采样到的概率是相同的。所以可以想到要想实现这样的转载 2017-03-12 15:09:23 · 727 阅读 · 0 评论 -
华为2017年机试_反转数字求和
题目: 反转数字求和。输入“123,456”,反转后求和:321+654=975,输出975。分析: 输入的是字符串,主要涉及到字符串到数字的转换,反转可用数字求余或字符串的反转。用到的函数:getline(cin, str)//输入,以enter结束str.find(','); //返回逗号的位置string str1(str, pos, len); //字符串的构造stoi(str1)原创 2017-03-25 11:49:41 · 1261 阅读 · 0 评论 -
8皇后问题
bool check(vector<int> res) { for (int row = 0; row < res.size()-1; row++) { for (int col = row+1; col < res.size(); col++) { if (row - col == res[row] - res[col] || row - col =原创 2017-05-13 16:29:17 · 436 阅读 · 0 评论 -
LeetCode_283. Move Zeroes
283. Move Zeroes Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after原创 2017-02-01 16:20:49 · 418 阅读 · 0 评论 -
LeetCode_167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers suc原创 2017-01-31 21:31:39 · 310 阅读 · 0 评论 -
LeetCode_258. Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r转载 2017-01-31 20:44:55 · 245 阅读 · 0 评论 -
LeetCode_504. Base 7
504. Base 7Given an integer, return its base 7 string representation. Example 1:Input: 100Output: "202"Example 2:Input: -7Output: "-10"Note: The input wil be in range of [-1e7, 1e7].解题思路: 题意:将十进制数原创 2017-02-18 13:06:54 · 822 阅读 · 0 评论 -
LeetCode_448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an ArrayGiven an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusi原创 2017-02-12 11:05:59 · 272 阅读 · 0 评论 -
LeetCode_container-with-most-water
链接:https://www.nowcoder.com/practice/c97c1400a425438fb130f54fdcef0c57?tpId=46&tqId=29167&tPage=1&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking 来源:牛客网题目描述: Given n non-negative integers a1, a原创 2017-02-20 23:52:16 · 393 阅读 · 0 评论 -
LeetCode_476. Number Complement
476. Number ComplementGiven a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: 1. The given integer is guanteed to fit w原创 2017-01-24 17:17:13 · 618 阅读 · 0 评论 -
LeetCode_344. Reverse String
344. Reverse String Write a function that takes a string as input and returns the string reversed.Example Given s=”hello”, return “olleh”. C++:class Solution{public: string reverseString(string原创 2017-01-24 21:33:49 · 264 阅读 · 0 评论 -
LeetCode_485. Max Consecutive Ones
485. Max Consecutive Ones Given a binary array, find the maxumum number of consecutive 1s in this array. Example 1Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three di原创 2017-01-26 11:55:13 · 298 阅读 · 0 评论 -
LeetCode_463. Island Perimeter
463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). T转载 2017-01-28 20:50:11 · 261 阅读 · 0 评论 -
LeetCode_rotate-image
链接:https://www.nowcoder.com/profile/1503/codeBookDetail?submissionId=9042883 来源:牛客网题目描述: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow u原创 2017-02-22 23:24:20 · 348 阅读 · 0 评论 -
LeetCode_136. Single Number
136. Single Number Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it wi原创 2017-01-30 10:26:06 · 273 阅读 · 0 评论 -
LeetCode_292. Nim Game
292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last sto原创 2017-01-30 10:53:29 · 287 阅读 · 0 评论 -
LeetCode_492. Construct the Rectangle
492. Construct the Rectangle For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectang原创 2017-01-30 11:46:25 · 831 阅读 · 0 评论 -
LeetCode_371. Sum of Two Integers
371. Sum of Two Integers Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example: Given a = 1 and b = 2, return 3.解题思路:既然不能用内置的加减法,那就只能用位运算。 xor:可理解为不考原创 2017-01-31 17:48:19 · 239 阅读 · 0 评论 -
层次遍历构建二叉树
struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};TreeNode* createTree(vector<string> nodes) { int len原创 2017-05-22 21:00:49 · 2816 阅读 · 3 评论