一、两数之和

题目链接:两数之和

1、题目描述

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出和为目标值target 的那两个整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例2:

输入:nums = [3,2,4], target = 6
输出:[1,2]

示例3:

输入:nums = [3,3], target = 6
输出:[0,1]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、题目分析

关键:每种输入只会对应一个答案
思路:我们在对数组进行遍历的时候,目标值target - 当前值currentNum = 满足条件值thatNum,只要我们在数组中找到满足条件的那个值的下标即可

public int[] twoSum(int[] nums, int target) {int[] result = new int[2];// 使用Map存储值和其对应的下标,方便快速查找Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {// 计算满足条件值int thatNum = target - nums[i];// 存在并且不是当前值,说明找到if (map.containsKey(thatNum) && i != map.get(thatNum)) {result[0] = i;result[1] = map.get(thatNum);return result;}map.put(nums[i], i);}return result;}

二、两数相加

题目链接:两数相加

1、题目描述

给你两个非空的链表,表示两个非负的整数。它们每位数字都是按照逆序的方式存储的,并且每个节点只能存储一位数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.

示例2:

输入:l1 = [0], l2 = [0]
输出:[0]

示例 3:

输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、题目分析

关键:每个节点只能存储一位数字
思路:两数相加的和sum,若sum>=10,则需要临时存储进位数字temp,并且temp会参与到下一次的加法运算中,然后在存储该结点上真正的值 sum%10
注意最后一次运算,若最后一次运算出现了进位的现象,则还需要把进位数字进行存储

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode l = new ListNode(-1, null);ListNode n = l;int temp = 0;while(l1 != null || l2 != null) {int num1 = l1 != null ? Integer.valueOf(l1.val) : 0;int num2 = l2 != null ? Integer.valueOf(l2.val) : 0;int sum = num1 + num2 + temp;// 进位temp = sum / 10;// 余数sum %= 10;ListNode node = new ListNode(sum, null);n.next = node;n = node;l1 = l1 != null ? l1.next : null;l2 = l2 != null ? l2.next : null;}if (temp != 0) {ListNode node = new ListNode(temp, null);n.next = node;}return l.next;}

三、无重复字符的最长子串

题目链接:无重复字符的最长子串

1、题目描述

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

示例 4:

输入: s = ""
输出: 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、题目分析

关键:不重复最长子串
思路:使用滑动窗口算法

public int lengthOfLongestSubstring(String s) {if (s.length() < 2) {return s.length();}// 初始化子串最大长度为1int max = 1;int left= 0;int length = s.length();// 记录子串Map<String, Integer> notRepeated = new HashMap<>();while(left < length - 1) {// 小优化,当窗口的长度已经小于当前最大长度了就停止滑动if (length - left < max) {break;}for (int right = left + 1; right <= length; right++) {// substring方法,包含left下标值,不包含right下标值String temp = s.substring(left, right);// 若存在已记录的重复子串,拦截if (notRepeated.containsKey(temp) && notRepeated.get(temp) == 1) {break;}// 若存在已记录的未重复子串,跳过当前循环if (notRepeated.containsKey(temp) && notRepeated.get(temp) == 0) {continue;}// 如果存在重复字串,就将此拦截if (!check(temp)) {// 记录重复子串notRepeated.put(temp, 1);// 上一个子串未重复,比对长度if (right - left - 1 > max) {max = right - left - 1;}break;}// 记录未重复子串notRepeated.put(temp, 0);// 窗口右侧到达底部仍未被拦截,需要进行更新if (right == length) {if (right - left > max) {max = right - left;}}}left++;}return max;}public boolean check(String str) {Set<Character> set = new HashSet<>();for (int i = 0; i < str.length(); i++) {set.add(str.charAt(i));}return set.size() == str.length();}

四、Z字形变换

题目链接:Z字形变换

1、题目描述

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “PAYPALISHIRING” 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

示例 1:

输入:s = "PAYPALISHIRING", numRows = 3
输出:"PAHNAPLSIIGYIR"

示例 2:

输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P     I    N
A   L S  I G
Y A   H R
P     I

示例 3:

输入:s = "A", numRows = 1
输出:"A"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zigzag-conversion
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、题目分析

关键:字符串s的长度与行数numRows可以变换成多少个完整的角,以及一个角所包含的字符个数
思路:首先根据数据计算出可以变换成多少个完成的角,以及一个角所包含的字符个数,一个角的字符数为一个周期,在一个周期内,分为竖列和斜列两部分,以行为单位,分别记录竖列和斜列(除了第一行和最后一行)上的字符,最后再整合所有行上的字符,就变换成功了

public String convert(String s, int numRows) {if (numRows == 1 || s.length() == 1) {return s;}int length = s.length();// 计算出角的数量,numRows + numRows - 2是每个完整角包含的字符数量,角的最后一个字符不是第一行int num = numRows + numRows - 2;int sum = length % num == 0 ? length / num : length / num + 1;// 每一行的起始坐标Map<Integer, String> rows = new HashMap<>();int[] start = new int[numRows];for (int i = 0; i < numRows; i++) {start[i] = i;rows.put(i, "");}// 当前所在行int currentRow = 0;// 当前周期int cycle = 1;// 当前周期执行次数int total = 0;while (cycle <= sum) {// 执行次数+1total++;String temp = rows.get(currentRow);// 竖列字符int index = start[currentRow] + (num * (cycle - 1));if (index < length) {temp = temp + s.charAt(index);}// 斜列字符,第一行和最后一行不存在斜列字符if (currentRow != 0 && currentRow != numRows - 1) {index = num * cycle - currentRow;if (index < length) {temp = temp + s.charAt(index);}}// 此周期中currentRow的字符全部加载完毕rows.put(currentRow, temp);currentRow++;// 一个周期完成,重置相关信息if (total == numRows) {currentRow = 0;total = 0;cycle++;}}StringBuilder result = new StringBuilder();for (int i = 0; i < numRows; i++) {result.append(rows.get(i));}return result.toString();}

五、整数反转

题目链接:整数反转

1、题目描述

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、题目描述

关键:反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0

public int reverse(int x) {String str = String.valueOf(x);if (str.charAt(0) == '-') {str = str.substring(1);}StringBuilder result = new StringBuilder();//  反转数字for (int i = str.length() - 1; i >= 0; i--) {result.append(str.charAt(i));}str = result.toString();long num = Long.parseLong(str);if (x < 0) {num = -num;}long max = (long) (Math.pow(2, 31) - 1);long min = (long) (-Math.pow(2, 31));if (num > max || num < min) {return 0;}return Integer.parseInt(String.valueOf(num));}

LeetCode刷题—第一周相关推荐

  1. 用JavaScript刷leetcode(刷题 第一天)

    两数之和 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是, ...

  2. leetCode刷题第一天--求两数之和

    两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...

  3. leetcode 刷题第二周 5月29-6月4号

    19. 删除链表中的倒数第n个节点 / /双指针的方法 class Solution { public:ListNode* removeNthFromEnd(ListNode* head, int n ...

  4. leetcode剑指offe刷题-第一题-用两个栈实现队列

    leetcode剑指offe刷题-第一题 文章目录 leetcode剑指offe刷题-第一题 前言 一.用两个栈实现队列 1.思路 2.代码如下 总结 前言 记录一下自己刷算法的路程. leetcod ...

  5. LeetCode刷题专栏第一篇--思维导图时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  6. LeetCode刷题1:第四周

    LeetCode刷题1:第四周 目录 LeetCode刷题1:第四周 一.前言 二.知识点 1.Python 字符串 Python 访问字符串中的值 Python 字符串更新 Python转义字符 P ...

  7. ​LeetCode刷题实战568:最大休假天数

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  8. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  9. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

最新文章

  1. LightTools( 32-64) 8.4.0下载与安装方法,lighttools免费版,lighttools(光学建模软件)【亲测有效】
  2. 让iis7.5显示php错误的详细信息~
  3. 图片浏览(CATransition)转场动画
  4. VTK:小部件之CaptionWidget
  5. 从构建分布式秒杀系统聊聊WebSocket推送通知
  6. c语言中table函数,Excel的TABLE函数是什么意思?
  7. 【python】-- 类的继承(新式类/经典类)、多态
  8. springmvc执行流程_SpringMVC
  9. SQL prompt无法激活跳转到127.0.0.1:22223的解决方案
  10. 在word文档里有一个向下的箭头,那是什么标志?
  11. 计算机文件右击怎么显示打开方式,怎么删除Win10文件右键菜单打开方式中已卸载程序选项?...
  12. 网站HTML SEO优化
  13. 【SQL2008】select TOP (100) PERCENT***********ORDER BY **********
  14. python协程详解
  15. 网格交易策略(附策略源码与收益图)
  16. 最短路及最短路计数(SPFA)
  17. 利用put、move的请求方式对IIS6.0服务器上传执行asp木马
  18. 英文星期的来历(都是来自神人)
  19. linux中shell的循环
  20. layui表单动态CURD功能

热门文章

  1. UltraISO Premium Edition v9.5.2.2836注册码
  2. 谭松波酒店评价情感分析代码以及数据集获取
  3. 黑客最可怕的7种攻击手段,到底厉害到什么程度?
  4. linux vnc 中文输入法,Linux:在VNC中使用SCIM输入法
  5. jmeter添加服务器cpu监控插件
  6. Html5画布绘制七巧板
  7. Windows版Git在进行git代码的时候出现报错
  8. mysql 双冒号_沙河壹佰
  9. 联想笔记本安装ubuntu18.04遇到的那些问题
  10. LINUX如何让两个不同网段的主机进行通信与虚拟机上网设置