摘要:
题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].思路:1. 阅读全文
摘要:
题目大整数乘法思路:1. 转化成分组背包问题, 代码比较 tricky2. 将 string 相乘转化为 string 按位乘再加的过程3. 细节. 不同长度 string 相加可以先补 0, 对齐. 按位相乘的时候, 可以从左向右计算, 便于移位代码:class Solution {public: string multiply(string num1, string num2) { if(num1.size() len2) { res.append(num1.size()-num2.size(), '0').append(num2); num2 = res; } re... 阅读全文
摘要:
题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0思路:1. 二分法变形, 最讨 阅读全文
摘要:
题目:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of i 阅读全文
摘要:
题目:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?update先转置, 后按列翻转class Solution {public: void rotate(vector > &matrix) { transpose(matrix); for(int i = 0; i > &matrix) { int n = matrix.size(... 阅读全文
摘要:
题目Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units o 阅读全文