2017-3-7 leetcode 66 119 121】的更多相关文章

今天纠结了一整天============================================================== leetcode66 https://leetcode.com/problems/plus-one/?tab=Description leetcode119 https://leetcode.com/problems/pascals-triangle-ii/?tab=Description leetcode121 https://leetcode.com/…
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. class Solution { public: int singleNumber(vector<int>& nums) { int len = nums.size(); ]; ; i < len; i++){…
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.…
2017/11/13 Leetcode 日记 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). The grid is completely surro…
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it withou…
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensiona…
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re…
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns the string reversed. class Solution { public: string reverseString(string s) { , sz = s.size()-; ; i < sz; i++, sz--){ char temp = s[i]; s[i] = s[sz];…
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit s…
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array.(根节点是数组中的最大值) The left subtree is the maximum tree const…
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,…
66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significan…
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at…
66. Plus One 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. 一个非负整数n按照从高到低的顺序将每一位放在数组中,然后n加1,返回加1后的数组. 注意进位的问题. 代码如下: class…
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? [思路] 我们为了满足空间复杂度的要求,我们新建两个ArrayList,一个负责存储上一个Pascal行的结果,一个根据上一个Pascal行得出当前P…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use onl…
题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 分析 与上题LeetCode 118 Pascal's Triangle本质相同: 需要注意的是,本题参数 k = 3 代表下标为3的那行元…
https://leetcode.com/problems/plus-one/ 题目: 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位,逢9进1.注意999这种情况要首位inser…
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assum…
66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. class Solution { public int[] plusOne(int[] digits…
###题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ) / 2 ) O(k * (k + 1) / 2) O(k∗(k+1)/2) 方法二:空间复杂度 O ( k ) O(k) O(k) 刷题心得 日期 [LeetCode] 题目地址:[https://leetcode.com/problems/pascals-triangle-ii/][1] T…
题意让大数加1 我的做法是先让个位+1,再倒置digits,然后进位,最后倒置digits,得到答案. class Solution { public: vector<int> plusOne(vector<int> &digits) { digits[digits.size() -]++; //个位+1 reverse(digits.begin(),digits.end());//倒置digits ; i < digits.size() - ; ++i){//除了最…
#-*- coding: UTF-8 -*- #Method1 :超时#class Solution(object):#    def maxProfit(self, prices):#      #        maxprofit=0#        for i in xrange(1,len(prices)):#            tmprofit=prices[i]-min(prices[0:i])#            maxprofit=tmprofit if tmprofit…
题目描述: 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. 解题思路: 设置进位,如果数组不够的话记得重新开辟空间. 代码如下: public class Solution { public int[…
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 提示: 此题要求给出杨辉三角对应行号下的所有数字序列,且空间复杂度为O(n).这里我们可以根据杨辉三角的定义,逐行计算.顺序从左到右或者从右到…
题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 思路: 题意:求对应的数字和字母换算之间的关系 1对应的该是第二个字母B,然而对应A,进位之后应该是BA,实际是AA,由此判断字母也是26进制,只是始终…
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assum…
class Solution: def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ s = '' for i in digits: # 转化成字符串 s += str(i) s = int(s) + 1 return [int(x) for x in str(s)] q = Solution() l1 = [9] l2 = [1, 2, 9] l…
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers A and B, return any string S such that: S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters; The substring 'aaa' does not…