leetcode 492-543 easy】的更多相关文章

题目: or 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 rectangular web page, whose length L and width W satisfy the following requirements…
492: 给定一个面积值,求它的长l和宽w.长和宽需满足:长大于等于宽,长和宽的差值尽可能小,长乘宽等于面积. 思路:先将l和w初始化为sqrt(area),然后看l*w是否等于面积,如果等于则返回l和w,若小则将l加一,若大则将w减一.…
1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设计的矩形页面必须等于给定的目标面积. 2. 宽度 W 不应大于长度 L,换言之,要求 L >= W . 3. 长度 L 和宽度 W 之间的差距应当尽可能小. 你需要按顺序输出你设计的页面的长度 L 和宽度 W. 示例: 输入: 4 输出: [2, 2] 解释: 目标面积是 4, 所有可能的构造方案有…
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe…
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7…
492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设计的矩形页面必须等于给定的目标面积. 宽度 W 不应大于长度 L,换言之,要求 L >= W . 长度 L 和宽度 W 之间的差距应当尽可能小. 你需要按顺序输出你设计的页面的长度 L 和宽度 W. 示例: 输入: 4 输出: [2, 2] 解释: 目标面积是 4, 所有可能的构造方案有 [1,4]…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode.com/problems/diameter-of-binary-tree/#/description 题目描述 Given a binary tree, you need to compute the length of the diameter of the tree. The diameter…
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 rectangular web page, whose length L and width W satisfy the following requirements: 1…
题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码:(1.22) /** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) {//给定两个参数nums,target var resu…
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 rectangular web page, whose length L and width W satisfy the following requirements: 1…
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte…
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题: 题目很简单,判断数组中是否含有重复数字,有重复则返回true,否则返回…
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.…
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. 解题: 题目比较简单,注意由于表头不确定,因此新建一个结点指向新创建的链表: 解题步骤: 1.判断l1和l2是否有效 2.新建preHead结点,newlist指针指向preHead: 3.循环开始,满足l1和l2都…
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目很简单,注意链表首结点有可能更改时,需新建preHead结点,或者使用二维指针的编程方法. /** * Defin…
题目: Reverse a singly linked list. 解题: 反转单链表,不再多介绍了. 如果会“先条件->定参数->确定不变式->验证后条件”的思维方法,一定会bug free. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class So…
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true…
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an…
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. 递归的解法: 用树递归的思想,将两个树以结点作为比较单位,只关注对当前位置结点的操作(是否都存在此结点,存在时值是否相同). 注意:…
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially…
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解题: 本题为典型的KMP算法考察题,KMP算法描述为: 设主串S,匹配串P,i为S的索引下标,j为P的索引下标,初始i和j设置为0. 在i小于S的长度和j小于P的长度时,开始循环: 1.比较S[i]和P[j]是否相同: 2.如果相同则i++,j+…
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解题: 将字符形式的罗马数字,转化为整形.输入在1~3999之间.罗马数字的书写规范,请参见罗马数字_百度百科: 本题的关键点在于,如何处理I.X.C三个数字放在大数左边是相减,放在大数右边是相加. 解法是,可以从输入字符串的末端开始,从右向左遍历字符串.对于出现的一般罗马字符,进行…
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"…
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 解题: 简单的运算后,可知此题为斐波那契数列生成题. 解题步骤: 1.新建三个初始变量step1 = 1,step2 = 2,result: 2.注意算法从n = 3开…
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 解题方法1: 模拟运算过程,从低位数字到高位数字计算,新建一个变量记录进位情况. class Solution { public: vector<int…
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e…
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are ma…
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII…
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" is not a palindrome. Note:Have you consider that th…
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's…