
算法
拼了命的珍惜
力气大的搬砖男子汉。
展开
-
算法设计:快速排序
template<class Type>void quickSort(Type a[],int l,int r){ if(l<r){ int p = partition(a,l,r); quickSort(a,l,p-1); quickSort(a,p+1,r); } template<class Type>int partition(Type a[],int l,int r){ int i=l,j=r+1; Type x=a[l]; while(原创 2020-07-27 22:05:07 · 462 阅读 · 0 评论 -
算法设计:合并排序
public void mergeSortDemo(int[] array){ mergeSort(array,0,array.length-1);}public void mergeSort(int[] array,int l,int r){ if(l>=r)return ;//当序列左边界大于等于右边界,结束 int mid = (l+r)>>1;//取中点 mergeSort(array,l,mid);//对左边序列递归分割 mergeSort(array,mid+1原创 2020-07-27 21:52:21 · 266 阅读 · 0 评论 -
算法设计:整数因子分解问题
对n的每个因子递归搜索。void solve(int n){ if(n == 1)total++; else{ for(int i=2; i<=n; i++)if(n%i == 0)solve(n/i); }}原创 2020-07-27 21:14:58 · 567 阅读 · 0 评论 -
【java-海明距离】477. Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given numbers.Example:Input: 4, 14, 2Output: 6Explanation: I原创 2020-07-05 15:58:41 · 225 阅读 · 0 评论 -
【java-动态规划】62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the dia原创 2020-07-05 15:06:53 · 240 阅读 · 0 评论 -
【split分割】468. Validate IP Address
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots原创 2020-07-03 22:15:19 · 225 阅读 · 0 评论 -
【java-快速幂】50. Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: 0.25000Explanation: 2-2 = 1/22 = 1/4 = 0.25Note:-100.原创 2020-07-03 12:26:16 · 238 阅读 · 0 评论 -
[快速排序]148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1->0->3->4->5/** * Definition for singly-linked lis原创 2020-07-02 22:31:25 · 200 阅读 · 0 评论 -
【暴力法】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 this character, takes an optional initial plus or minus sign原创 2020-07-01 23:17:08 · 230 阅读 · 0 评论 -
【大数库暴力法】7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an environment which could only store integers within the 3原创 2020-07-01 18:30:43 · 193 阅读 · 0 评论 -
【java大数库暴力】给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807class ListNode { int val;转载 2020-06-30 22:35:39 · 1305 阅读 · 0 评论