![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
java leetcode
whitesun123
这个作者很懒,什么都没留下…
展开
-
Given an array of integers, return indices of the two numbers such that they add up to a specific ta
问题是要从数组中找到两个数据,使得两数之和等于目标值,输出该两数的下标(从0开始)方法一:public static int[] twoSum_2(int[] num,int target ) { int low ,high; int temp; for(int i = 0; i< num.length; i++){ low = i; temp = targ...原创 2017-04-14 21:23:26 · 1675 阅读 · 0 评论 -
旋转数组
将包含 n 个元素的数组向右旋转 k 步。例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4]。1.解法一:借助O(n)O(n)的空间解法。再利用(i + k) % n = 旋转后的位置,可以将原有数组中的数据复制到新数组中。public int[] rotate(int[] nums, int k) { ...原创 2018-04-12 10:16:39 · 140 阅读 · 0 评论 -
反转字符串
请编写一个函数,其功能是将输入的字符串反转过来。解法一:转为数组public String reverseString(String s) { char[] a = s.toCharArray(); /* System.out.println(a[1]);*/ int start = 0; int end = a.length-1...原创 2018-04-12 11:31:20 · 298 阅读 · 0 评论 -
数组存在重复元素
给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数应该返回 true。如果每个元素都不相同,则返回 false。思路:判断重复,首先就想到了Set,创建一个set,把每个数组元素放进去判断是否重复,解法一:判断重复,首先就想到了Set,创建一个set,把每个数组元素放进去判断是否重复,public boolean containsDuplicate(int[] nums)...原创 2018-04-06 12:50:03 · 3236 阅读 · 0 评论 -
颠倒整数
给定一个范围为 32 位 int 的整数,将其颠倒。例 1:输入: 123输出: 321思路一:public int reverse(int x) { int a; long sum = 0; while(x!=0){ a = x%10; sum = sum*10+a; x...原创 2018-04-12 19:07:31 · 207 阅读 · 0 评论 -
字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。案例:s = "leetcode"返回 0.s = "loveleetcode",返回 2.思路一:若不要求时间,那么较简单public int firstUniqChar(String s) {//时间复杂度o(n^2) char[] c = s.toCharArray(); //...原创 2018-04-12 19:11:11 · 2623 阅读 · 1 评论 -
只出现一次的数字
给定一个整数数组,除了某个元素外其余元素均出现两次。请找出这个只出现一次的元素。 备注:你的算法应该是一个线性时间复杂度。 你可以不用额外空间来实现它吗?思路:虽然可以记录每个数字出现的次数,但是不让用额外空间实现,想了半天也想不出来什么 讨巧的方法,最后搜了一下别人的思路,震惊我了,原来还可以这样.解法一:巧用异或 public int singleNumber(int[] nums) { ...转载 2018-04-07 13:31:24 · 104 阅读 · 0 评论 -
数数并说(Count And Say)
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:1. 12. 113. 214. 12115. 111221给定一个正整数 n ,输出报数序列的第 n 项。注意:整数顺序将表示为一个字符串。解法一:乱糟糟版public String countAndSay(int n) { if( n == 1)...原创 2018-04-19 09:46:45 · 3023 阅读 · 0 评论 -
有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词。例如,s = "anagram",t = "nagaram",返回 trues = "rat",t = "car",返回 false思路一:public boolean isAnagram(String s, String t) { if(s.length() != t.length()) ...原创 2018-04-13 16:43:57 · 408 阅读 · 0 评论 -
删除链表指定位置节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n = 2.当删除了倒数第二个节点后,链表变为 1->2->3->5.进阶:你能尝试使用一趟扫描实现吗?思路一:先得到一链表大小public ListNode removeNthFromEnd_2(ListNode head, in...原创 2018-04-20 14:13:51 · 2021 阅读 · 0 评论 -
删除指定节点
请编写一个函数,使其可以删除某个链表中给定的(非末尾的)节点,您将只被给予要求被删除的节点。比如:假设该链表为 1 -> 2 -> 3 -> 4 ,给定您的为该链表中值为 3 的第三个节点,那么在调用了您的函数之后,该链表则应变成 1 -> 2 -> 4 。思路: 一般我们删除某个节点都要知道前一个节点,现在不知道前一个节点,那么就赋值,假" 删除"这个节点,实际删...原创 2018-04-20 14:18:46 · 1289 阅读 · 0 评论 -
反转单链表
反转一个单链表。进阶:链表可以迭代或递归地反转。你能否两个都实现一遍?解法一:使用额外空间,比如数组,stack等解法二:迭代解法定义3个指针,分别指向当前遍历到的结点、它的前一个结点及后一个结点。在遍历过程中,首先记录当前节点的后一个节点,然后将当前节点的后一个节点指向前一个节点,其次前一个节点再指向当前节点,最后再将当前节点指向最初记录的后一个节点,如此反复,直到当前节点的后一个节点为NULL...原创 2018-04-20 16:02:14 · 213 阅读 · 0 评论 -
合并两个有序链表
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例:输入:1->2->4, 1->3->4输出:1->1->2->3->4->4思路一:用其他数组呀什么的...思路二:public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ...原创 2018-04-21 15:03:33 · 133 阅读 · 0 评论 -
合并有序数组
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 nums...原创 2018-06-09 11:21:27 · 287 阅读 · 0 评论 -
猴子报数选国王问题
问题: 猴子选大王,假设有M个猴子围成一圈,从1开始报数,数到N的猴子退出,最后剩下的猴子就是大王, 写出程序求出最后是大王的那只猴子最初的位置。解析:问题本来只需要求解剩下猴子的序号,现在提出一个要求,要求把呗淘汰猴子的顺序记录下来,只需要再创建一个List即可import java.util.ArrayList;import java.util.List;/* * * 猴...原创 2018-09-16 09:44:40 · 1447 阅读 · 0 评论 -
Move Zero
给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, 12, 0, 0]。注意事项:必须在原数组上操作,不要为一个新数组分配额外空间。尽量减少操作总数。思路:本来是想从新排序,可是想不出来好的方法,那就先用一个特殊技巧方法一:只保留非零数,后面在加上零...原创 2018-03-29 15:56:19 · 229 阅读 · 0 评论 -
Implement strStr
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。示例 1:输入: haystack = "hello", needle = "ll"输出: 2思路一:这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子...原创 2018-04-17 15:54:35 · 142 阅读 · 0 评论 -
Write a function to find the longest common prefix string amongst an array of strings.
给定一个String类型数组,要求写一个方法,返回数组中这些字符串的最长公共前缀。方法一:import java.util.Scanner;import java.util.logging.Logger;import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;public class Solution { public sta...原创 2017-04-14 20:43:45 · 522 阅读 · 0 评论 -
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 bu...原创 2017-04-14 18:28:56 · 159 阅读 · 0 评论 -
Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to find ...原创 2017-05-17 21:45:10 · 208 阅读 · 0 评论 -
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-03-18 16:51:03 · 117 阅读 · 0 评论 -
Max Area of Island
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrou...转载 2018-03-22 14:52:59 · 166 阅读 · 0 评论 -
Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. 注意点: 装箱拆箱 异常抛出 int类型最小数的负值问题 1.数学转换public static int reverse(int x) { long b = 0; long c ; c ...原创 2018-03-19 17:02:28 · 126 阅读 · 0 评论 -
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(ie, buy one and sell one share of the stock),d...原创 2018-03-23 14:43:38 · 88 阅读 · 0 评论 -
Rmove dumplicates
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying th...原创 2018-03-20 13:59:20 · 373 阅读 · 0 评论 -
Isvalid Sudo
判断一个数独是否有效,说明:一个有效的数独(填了一部分的)不一定是可解的,只要已经填的数字是有效的即可。分析:该题目不要判断整个数独是否有解,只需要判断当前给出的数独是否有效。因此只需要判断行和列是否有效,判断每个块是否有效。而判断一行中是否有重复的数字,最好的数据结构莫过于Set结构了。使用rowSet,colSet两个Set结构来分别保存当前遍历的行和列,(i, j)表示行,则(j, i)就表...转载 2018-03-30 14:38:32 · 153 阅读 · 0 评论 -
Remone_Element
Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-plac...转载 2018-03-25 11:49:07 · 147 阅读 · 0 评论 -
两个数组的交集 | ,||
给定两个数组,写一个方法来计算它们的交集注意: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。 我们可以不考虑输出结果的顺序。跟进:如果给定的数组已经排好序呢?你将如何优化你的算法?如果 nums1 的大小比 nums2 小很多,哪种方法更优?如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存中,你该怎么办?一: 给定 nums1 = [1,...原创 2018-04-08 12:37:18 · 433 阅读 · 0 评论 -
PLus One
给定一个非负整数组成的非空数组,给整数加一。可以假设整数不包含任何前导零,除了数字0本身。最高位数字存放在列表的首位。解析:大数加法问题,不能先转换为数加一后在转换为数组一个数组每一个数都是9,那么加一后长度会加一,后面都是0;如果不满足每一个数都是9,那么长度不会加一,那么遍历每个数,如果是九,那么这一位变为零,高位加一就是加一的结果java: public int[] plusOne(int[...原创 2018-03-27 19:44:03 · 127 阅读 · 0 评论 -
字符串转整数(atoi)
/*实现 atoi,将字符串转为整数。提示:仔细考虑所有的输入情况。如果你想挑战自己,请不要看下面的示例,并尽可能独立思考所有的情况。说明:这道题解释的比较模糊(即没有指定输入格式)。你需要事先汇总所有的输入情况。*/ /*字符串题一般考查的都是边界条件、特殊情况的处理。所以遇到此题一定要问清楚各种条件下的输入输出应该是什么样的。这里已知的特殊情况有:能够排除首部的空格,从第一个非空字符开始计算允...原创 2018-04-16 16:46:06 · 202 阅读 · 0 评论 -
java 程序题 判断两个字符串是否是同构的
两个个字符串的每个字母都匹配同一个映射关系,比如egg -> add的映射关系就是:e->a, g->d; foo与bar显然不满足,因为o->a同事o->r;paper和title满足,p->t, a->i, e->l, r->e。现在用map保存映射关系方法一:public static boolean isIsomorphic(...原创 2018-09-19 11:43:00 · 1032 阅读 · 0 评论