
leetcode
xcy14
这个作者很懒,什么都没留下…
展开
-
leetcode1300:转变数组后最接近目标值的数组和
思路如下:先把数组从小到大排序,然后依次遍历数组,当前index为i,前0到i-1个数字的和为s,则剩余的值为target-s,假如当前arr[i]*(len(arr)-i)已经大于target-s了,则说明可以从当前第i个值开始,将所有之后的值均设置为同一个值,来逼近target,如果小于target-s,则说明即使从当前第i个值开始,将所有之后的值都设置为同一个值,也依旧会小于target,不是最优解。当大于target-s,则需要有一个边界判断,mean=target-s/(len(arr)-i),原创 2020-06-14 23:49:02 · 161 阅读 · 0 评论 -
leetcode99. Recover Binary Search Tree
题目Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a c原创 2017-10-06 17:22:52 · 157 阅读 · 0 评论 -
leetcode395. Longest Substring with At Least K Repeating Characters
题目Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.Example 1:Input:s = "aaabb", k = 3Output原创 2017-10-06 20:20:08 · 163 阅读 · 0 评论 -
leetcode621. Task Scheduler
题目Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could原创 2017-10-06 21:14:01 · 395 阅读 · 0 评论 -
leetcode225.Implement Stack using Queues
题目Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2017-10-06 01:12:02 · 179 阅读 · 0 评论 -
leetcode481.Magical String
A magical string S consists of only ‘1’ and ‘2’ and obeys the following rules:The string S is magical because concatenating the number of contiguous occurrences of characters ‘1’ and ‘2’ generates the原创 2017-10-05 16:50:46 · 174 阅读 · 0 评论 -
leetcode142.Linked List Cycle II
题目Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using extra space? 解法:题目的意思就是判断原创 2017-10-06 00:13:08 · 174 阅读 · 0 评论 -
leetcode164.Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements.You may原创 2017-10-05 18:55:58 · 241 阅读 · 0 评论 -
leetcode147. Insertion Sort List
题目Sort a linked list using insertion sort.解法用插入排序对链表进行排序。遍历链表,每次将当前结点插入到已经排序好的列表。代码/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(原创 2017-10-09 20:42:54 · 164 阅读 · 0 评论