
LeetCode
hf寒沨
Coding...
展开
-
LeetCode: 9. Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.大致意思:判断整数是否回文,不需要额外空间。对于“Do this without extra space” 我以为是不需要再申请变量空间,懵逼》》》查看solution中发现还是有申请变量,OK!class Solution { publi...原创 2018-04-02 09:48:45 · 201 阅读 · 0 评论 -
【LeetCode】80. Remove Duplicates from Sorted Array II
Problem:Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this ...原创 2018-05-02 10:45:47 · 172 阅读 · 0 评论 -
【LeetCode】725. Split Linked List in Parts
Problem:Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".The length of each part should be as equal as possible: no tw...原创 2018-05-09 15:14:07 · 333 阅读 · 3 评论 -
【LeetCode】338. Counting Bits
Problem:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5...原创 2018-04-25 10:30:53 · 157 阅读 · 0 评论 -
【LeetCode】238. Product of Array Except Self
Problem:Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and i...原创 2018-04-25 11:28:48 · 145 阅读 · 0 评论 -
【LeetCode】328. Odd Even Linked List
Problem:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do i...原创 2018-05-04 16:27:50 · 375 阅读 · 0 评论 -
【LeetCode】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 li...原创 2018-05-04 16:35:14 · 139 阅读 · 0 评论 -
【LeetCode】92. Reverse Linked List II
Problem:Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5...原创 2018-05-04 16:51:46 · 151 阅读 · 0 评论 -
【LeetCode】234. Palindrome Linked List
Problem: 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? 题目:给定一个单链表,判断是否为回文?最好时间复杂度O(n),空间复杂度为 O(1)。 思路:除去要求,最简单的方法则是用stack...原创 2018-05-05 21:00:24 · 220 阅读 · 0 评论 -
【LeetCode】199. Binary Tree Right Side View
Problem:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 题目:给定一个二叉树,输出从右侧看到的节点(大致就是每层的最右侧的节点). 思路:做的二叉...原创 2018-10-29 15:55:00 · 157 阅读 · 0 评论 -
【LeetCode】209. Minimum Size Subarray Sum
Problem:Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. 题目:给定一个长度为n正整数数组和一...原创 2018-12-09 22:31:41 · 156 阅读 · 0 评论 -
【数据结构】Trie Tree:字典树(前缀树)的实现
字典树又称为前缀树或Trie树,是处理字符串常见的数据结构。假设组成所有单词的字符仅为a-z。 字典树介绍 字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间,比如加入"abc"、“abcd”、“abd”、“b”、“bcd”、“efg”、"hik"之后,字典树如图所示。 基本特性 根节点没有字符路径。除了根节点外,每一个节点都被一个字符路径找到。 从根节点到某一节点,将路径上经过的...原创 2019-01-19 15:54:31 · 790 阅读 · 0 评论 -
【LeetCode】82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example 1:Input: 1->2->3->3->4->4->5 Output: 1->2-&g...原创 2018-05-02 00:43:39 · 110 阅读 · 0 评论 -
【LeetCode】3. Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", wi...原创 2018-05-01 17:15:54 · 131 阅读 · 0 评论 -
LeetCode: 14. Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.找出字符串数组最长的公共前缀Solution中的算法:class Solution { public String longestCommonPrefix(String[] strs) { if (st...原创 2018-04-02 09:52:34 · 145 阅读 · 0 评论 -
LeetCode: 20. Valid Parentheses
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 ...原创 2018-04-02 09:53:22 · 114 阅读 · 0 评论 -
LeetCode: 136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra mem...原创 2018-04-02 09:54:10 · 121 阅读 · 0 评论 -
LeetCode:344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".题意:将一个String字符串逆转。思路:直接遍历逆转输出。嗯,恭喜,超时!!!懵逼,看大神,使用内置函数StringBuilder。在此之前,我以为内置函数的...原创 2018-04-02 09:54:56 · 167 阅读 · 0 评论 -
【LeetCode】 69. Sqrt(x)
题目:求平方根。。。必须返回的是整数,就是小数点要舍弃。思路:直接用轮子。。。sqrt()代码:class Solution { public int mySqrt(int x) { return (int)Math.sqrt(x); }}...原创 2018-04-03 22:44:46 · 159 阅读 · 0 评论 -
【LeetCode】461. Hamming Distance
Problem:The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.题目:求汉明距离,即两个数不同位数的...原创 2018-04-23 09:26:34 · 185 阅读 · 0 评论 -
【LeetCode】448. Find All Numbers Disappeared in an Array
Problem:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array....原创 2018-04-23 10:31:26 · 135 阅读 · 0 评论 -
【LeetCode】771. Jewels and Stones
Problem:You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in Sis a type of stone you have. You want to know how many of ...原创 2018-04-23 11:47:00 · 233 阅读 · 0 评论 -
【LeetCode】561. Array Partition I
Problem:Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large...原创 2018-04-23 14:30:20 · 145 阅读 · 0 评论 -
【LeetCode】821. Shortest Distance to a Character
Problem:Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.题目:给定一个字符串S和字符C,返回一个整型数组,数组每一位表示字符串对应下标位字符距离C字符的最短距离。思路:首先...原创 2018-04-23 15:52:34 · 513 阅读 · 2 评论 -
【LeetCode】371. Sum of Two Integers
Problem:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.题目:计算a和b的和,不使用加减运算符。思路:使用异或,与等逻辑运算。(参考计算机组成原理中补码加减法等)使用异或...原创 2018-04-23 16:56:41 · 117 阅读 · 0 评论 -
【算法】回溯算法
百度百科 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。 回溯法是一种 选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”。许多复杂的,规模较大的问题都可以使用回溯法...原创 2019-01-10 11:11:58 · 217 阅读 · 0 评论