LeetCode
文章平均质量分 59
muqingwind
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode - 26 删除排序数组中的重复项
题目给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。思路1 不需要额外数组,直接修改原数组,所以不能new int[array.length]记录独立的数值2 需要记录一个索引index来保存确定好了的不重复的位置3 num[i++] 这种表示都是先试用后加,...原创 2018-04-18 20:56:06 · 463 阅读 · 0 评论 -
LeetCode 237 Delete Node in a Linked List
题目Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node wit...原创 2018-04-25 12:17:18 · 148 阅读 · 0 评论 -
19. Remove Nth Node From End of List
题目Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the ...原创 2018-04-25 12:39:38 · 150 阅读 · 0 评论 -
21. Merge Two Sorted Lists
题目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.Example:Input: 1->2->4, 1->3->4Output: 1->...原创 2018-04-25 13:50:34 · 135 阅读 · 0 评论 -
234. Palindrome Linked List
题目Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?分析1 题目要求 time O(n) space O(1), 意味着要在原链表操作2 对于链表的回文串的判断,只能通过翻转 然后两个链表再从头到尾的进行比较 翻转...原创 2018-04-25 15:08:05 · 139 阅读 · 0 评论 -
141. Linked List Cycle
题目Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?分析1 判断链表是否有环 way 1 快慢指针 way 2 每遍历一个结点都将这个结点指向本身,如果再次遍历到指向其本身的结点...原创 2018-04-25 20:22:43 · 125 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
题目Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no children....原创 2018-04-25 20:44:39 · 151 阅读 · 0 评论 -
98. Validate Binary Search Tree (inorder traversal)
题目Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The righ...原创 2018-04-26 01:31:10 · 158 阅读 · 0 评论 -
101. Symmetric Tree
题目Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But t...原创 2018-04-26 01:35:55 · 172 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal (二叉树层次遍历)
题目Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / ...原创 2018-04-26 01:58:15 · 328 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
题目Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the...原创 2018-04-26 13:11:57 · 256 阅读 · 0 评论 -
88. Merge Sorted Array
题目Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively.You may assume that nu...原创 2018-04-26 13:56:29 · 145 阅读 · 0 评论 -
278. First Bad Version
题目You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on t...原创 2018-04-26 14:12:57 · 330 阅读 · 0 评论 -
70. Climbing Stairs
问题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?Note: Given n will be a positive i...原创 2018-04-26 15:21:21 · 215 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
问题Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock)...原创 2018-04-26 15:45:14 · 201 阅读 · 0 评论 -
LeetCode 203 删除链表中的元素
题目删除链表中等于给定值 val 的所有元素。示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --> 2 --> 3 --> 4 --> 5分析1 第一道链表结构的题2 两种思路 1 递归的方式来做 2 创造一个结点,while循环的方式来做...原创 2018-04-25 12:09:00 · 171 阅读 · 0 评论 -
LeetCode - 138 只出现一次的数字
题目给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?分析1 本来想的是用Java里的排序,排完顺序之后,再分第一个位置 和中间位置处理, 最后返回最后一个位置 这样的做法显然不是很好,不说用到了Java里的arrays.sort 在找出唯一的那个不是出现两次的元素用到的方法...原创 2018-04-25 11:55:32 · 626 阅读 · 0 评论 -
LeetCode 217 重复的数
tips:1 两次循环 2 arrays.sort3 hashSet set 类比Arraylist 数组 set.contains(i) set.add list.get(idx)题目给定两个数组,写一个方法来计算它们的交集。例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意...原创 2018-04-24 01:23:57 · 137 阅读 · 0 评论 -
LeetCode 66 Plus one
题目给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。分析1 从末尾开始遍历 如果是9 变为0 否则加1返回2 如果遍历完都没有返回 说明是类似999,这时构造一个新数组 array.length + 1代码class Solution { public in...原创 2018-04-24 01:45:24 · 155 阅读 · 0 评论 -
LeetCode 288 移动0
题目给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, 12, 0, 0]。注意事项:必须在原数组上操作,不要为一个新数组分配额外空间。尽量减少操作总数分析1 只需要维护一个id, 这个id记录当前已经确定好的元素的位置,id++ 这里有个处理...原创 2018-04-24 10:47:58 · 267 阅读 · 0 评论 -
LeetCode 1 two Sum
问题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 ...原创 2018-04-24 11:27:55 · 151 阅读 · 0 评论 -
LeetCode - 206 翻转链表
题目反转一个单链表。思路1 两种方案,递归和非递归2 这里用到了链表的数据结构,注意数据结构的定义,包括构造函数等3 递归方案 递归函数传递两个参数,开始是头结点和null 1 先判断head是否为空节点,空直接返回null 2 next 保存 head 的下个结点, head指向为空 ...原创 2018-04-19 20:42:06 · 438 阅读 · 0 评论 -
LeetCode - 189 旋转数组
题目将包含 n 个元素的数组向右旋转 k 步。例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4]。注意:尽可能找到更多的解决方案,这里最少有三种不同的方法解决这个问题。思路1 题目比较好理解,但是自己在第一次写的时候却不是那么容易,关键是理清关系2 对于way 1 首先是k, 简单通俗的将 k为3时, 数组...原创 2018-04-19 21:26:00 · 525 阅读 · 0 评论 -
LeetCode - 344 翻转字符串
题目请编写一个函数,其功能是将输入的字符串反转过来。例 hello -- olleh分析1 这里用到Java里的string常用的处理方式 tochararray将string处理为char类型的数组,一开始想直接在原string上调用.charat来处理,结果这样是不可行的。所以第一步是将string转换成char数组2 翻转这个char数组 常用的while循环3 char数组转换成strin...原创 2018-04-19 21:44:08 · 1845 阅读 · 0 评论 -
LeetCode 48 Rotate Image
题目You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix direc...原创 2018-04-24 11:46:22 · 155 阅读 · 0 评论 -
LeetCode 387 字符串中唯一的字符
题目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 = "loveleetcode",return 2.Note: You may assu...原创 2018-04-24 12:55:29 · 175 阅读 · 0 评论 -
LeetCode 125 验证回文串
问题Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.Example...原创 2018-04-24 16:10:54 · 4034 阅读 · 0 评论 -
LeetCode 8 String to Integer (atoi)
问题Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from thi...原创 2018-04-24 17:35:36 · 159 阅读 · 0 评论 -
LeetCode 28 Implement strStr()
问题Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.分析1 可以理解为字符串匹配题2 如果needle为空怎么办,直接返回03 time o(m*n) 4 for循环中不加条件语句,永为真 ,函数可以不...原创 2018-04-24 18:20:48 · 175 阅读 · 0 评论 -
LeetCode 14 Longest Common Prefix
题目Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".分析1 注意是最长公共前缀,一开始没注意到这个条件浪费了一些时间2 Java里的两个字符串的匹配函数 index...原创 2018-04-25 11:26:05 · 203 阅读 · 0 评论 -
LeetCode -122- 两次买卖问题
贪心算法初始值 到 结束值的距离就在那儿无论一步走,多步走的结果都是一样的原创 2018-04-25 11:26:38 · 331 阅读 · 0 评论 -
53. Maximum Subarray(DP)
问题Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation:...原创 2018-04-26 16:25:39 · 362 阅读 · 0 评论
分享