大家好,我是方圆
无它,唯手熟尔

文章目录

  • 快慢指针遍历
    • 141. 环形链表
    • 202. 快乐数
    • 876. 链表的中间结点
  • 区间合并
    • 56. 合并区间
  • 字符串操作
    • 6. Z字形变换
    • 14. 最长公共前缀
    • 763. 划分字母区间
  • 数字操作
    • 7. 整数反转
    • 8. 字符串转换
    • 9. 回文数
    • 43. 字符串相乘
    • 172. 阶乘后的零
    • 258. 各位相加

快慢指针遍历

141. 环形链表

public class Solution {public boolean hasCycle(ListNode head) {if(head == null || head.next == null)return false;ListNode slow = head,fast = head.next;while(slow != fast){if(fast == null || fast.next == null)return false;slow = slow.next;fast = fast.next.next;}return true;}
}

202. 快乐数

class Solution {public boolean isHappy(int n) {int slow = n, fast = getNext(n);while(fast != 1) {if(slow == fast) return false;slow = getNext(slow);fast = getNext(getNext(fast));}return true;}private int getNext(int n) {int sum = 0;while(n != 0) {int x = n % 10;sum += x * x;n /= 10;}return sum;}
}

876. 链表的中间结点

class Solution {public ListNode middleNode(ListNode head) {ListNode slow = head, fast = head;while(fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;}return slow;}
}

区间合并

56. 合并区间

class Solution {public int[][] merge(int[][] intervals) {List<int[]> res = new ArrayList<>();if(intervals == null || intervals.length == 0)return res.toArray(new int[0][]);Arrays.sort(intervals,(x,y) -> x[0] - y[0]);for(int i = 0; i < intervals.length; i++) {int left = intervals[i][0];int right = intervals[i][1];//注意此处是大于等于while(i < intervals.length - 1 && right >= intervals[i + 1][0]) {i++;right = Math.max(right,intervals[i][1]);}res.add(new int[] {left,right});}return res.toArray(new int[0][]);}
}

字符串操作

6. Z字形变换

//技巧性挺强的
class Solution {public String convert(String s, int numRows) {if(numRows == 1) return s;int len = Math.min(s.length(),numRows);String[] rows = new String[len];for(int i = 0; i < rows.length; i++) {rows[i] = "";}boolean down = true;int temp = 0;for(int j = 0; j < s.length(); j++) {if(down) rows[temp++] += s.substring(j,j + 1);else rows[temp--] += s.substring(j,j + 1);if(temp == 0 || temp == len - 1) down = !down;}String res = "";for(String str : rows) res += str;return res;}
}

14. 最长公共前缀

class Solution {public String longestCommonPrefix(String[] strs) {if(strs == null || strs.length == 0) return "";String res = strs[0];for(int i = 1; i < strs.length; i++) {int j = 0;for(; j < res.length() && j < strs[i].length(); j++)if(res.charAt(j) != strs[i].charAt(j)) break;res = res.substring(0,j);if(res.equals("")) return "";}return res;}
}

763. 划分字母区间

//这个技巧性也挺强的
class Solution {public List<Integer> partitionLabels(String S) {List<Integer> res = new ArrayList<>();int[] last = new int[26];for(int i = 0; i < S.length(); i++) {last[S.charAt(i) - 'a'] = i;}int head = 0,tail = 0;for(int i = 0; i < S.length(); i++) {tail = Math.max(tail,last[S.charAt(i) - 'a']);if(i == tail) {res.add(tail - head + 1);head = tail + 1;}}return res;}
}

数字操作

7. 整数反转

//越界判断
class Solution {public int reverse(int x) {int res = 0;int bundry = Integer.MAX_VALUE / 10;while(x != 0) {int n = x % 10;x /= 10;if(Math.abs(res) > bundry || (res == bundry && n > 7))return 0;res = res * 10 + n;}return res;}
}

8. 字符串转换

class Solution {public int myAtoi(String str) {int res = 0;str = str.trim();if(str.length() == 0) return 0;boolean flag = true;int cur = 0;if(str.charAt(0) == '-') {flag = false;cur = 1;}else if(str.charAt(0) == '+') {cur = 1;}int bundry = Integer.MAX_VALUE / 10;for(int i = cur; i < str.length(); i++) {char n = str.charAt(i);if(n > '9' || n < '0') break;if(res > bundry || (res == bundry && n > '7'))return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;res = res * 10 + (n - '0');}return flag ? res : -res;}
}

9. 回文数

class Solution {public boolean isPalindrome(int x) {if(x < 0) return false;int rex = 0,temp = x;while(temp != 0) {int n = temp % 10;temp /= 10;rex = rex * 10 + n;}if(rex == x) return true;else return false;}
}

43. 字符串相乘

class Solution {public String multiply(String num1, String num2) {if(num1.equals("0") || num2.equals("0")) return "0";int[] res = new int[num1.length() + num2.length()];for(int i = num1.length() - 1; i >= 0 ; i--) {int n1 = num1.charAt(i) - '0';for(int j = num2.length() - 1; j >= 0; j--) {int n2 = num2.charAt(j) - '0';int sum = res[i + j + 1] + n1 * n2;res[i + j + 1] = sum % 10;res[i + j] += sum / 10;}}StringBuilder ans = new StringBuilder();for(int i = 0; i < res.length; i++) {if(i == 0 && res[0] == 0) continue;ans.append(res[i]);}return ans.toString();}
}

172. 阶乘后的零

class Solution {public int trailingZeroes(int n) {int count = 0;while(n > 0){count += n / 5;n /= 5;}return count;}
}

258. 各位相加

class Solution {public int addDigits(int num) {if(num < 10) return num;int next = 0;while(num != 0){next += num % 10;num /= 10;}return addDigits(next);}
}

加油儿!!!

LeetCode,无它,唯手熟尔(二)相关推荐

  1. 樊登读书搞定读后感_读书笔记/读后感《读懂一本书,樊登读书法》——无他唯手熟尔...

    当我们在生活及工作中遇到问题的时候,通常会请教同事或者上网搜索解决方法,但是我们忽略了另外一个解决问题的途径,那就是通过阅读书籍积累知识.为什么大家并不习惯于阅读书籍呢,那是因为大家认为眼前遇到问题最 ...

  2. 1929. 数组串联【我亦无他唯手熟尔】

    1929. 数组串联 题目 题解 题目 给你一个长度为 n 的整数数组 nums .请你构建一个长度为 2n 的答案数组 ans ,数组下标 从 0 开始计数 ,对于所有 0 <= i < ...

  3. 786. 第 K 个最小的素数分数【我亦无他唯手熟尔】

    786. 第 K 个最小的素数分数 786. 第 K 个最小的素数分数 题解 786. 第 K 个最小的素数分数 难度 困难 给你一个按递增顺序排序的数组 arr 和一个整数 k .数组 arr 由 ...

  4. 237. 删除链表中的节点【我亦无他唯手熟尔】

    237. 删除链表中的节点 请编写一个函数,用于 删除单链表中某个特定节点 .在设计函数时需要注意,你无法访问链表的头节点 head ,只能直接访问 要被删除的节点 . 题目数据保证需要删除的节点 不 ...

  5. 438. 找到字符串中所有字母异位词【我亦无他唯手熟尔】

    438. 找到字符串中所有字母异位词 438. 找到字符串中所有字母异位词 题解 官方 438. 找到字符串中所有字母异位词 难度 中等 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 ...

  6. 136. 只出现一次的数字【我亦无他唯手熟尔】

    136. 只出现一次的数字 136. 只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素.说明:你的算法应该具有线性时间复杂度. 你 ...

  7. 卖油翁 欧阳修 无他,但手熟尔

    陈康肃公尧咨善射,当世无双,公亦以此自矜.尝射于家圃,有卖油翁释担而立,睨之,久而不去.见其发矢十中八九,但微颔之. 康肃问曰:"汝亦知射乎?吾射不亦精乎?"翁曰:"无他 ...

  8. 算法唯手熟尔(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/3/5 PAT A1119_C 2020/3/6 PAT A1123_C PAT A1115_C PAT A1114_C leetcode 206. 反转链表_C leetcode ...

  9. 算法唯手熟尔4月(PAT剩余清单 or leetcode)---希望可以日更

    文章目录 2020/4/1 leetcode 914. 卡牌分组_Go leetcode 1111. 有效括号的嵌套深度_Go 2020/4/2 leetcode 289. 生命游戏_Go 2020/ ...

  10. 编程思想:我亦无他,唯手熟尔

    目录 一.编程思想三大定理 二.编程思想四大特性 三.做到高内聚,低耦合 (一)内聚 (二)耦合 一.编程思想三大定理 (1)思路分析,化繁为简:将需求拆成多个步骤实现,先完成基本功能和大致框架,在往 ...

最新文章

  1. mybatis中传入String类型参数的问题
  2. mfc中嵌入python_Python 中的 Hook 钩子函数
  3. idea安装Maven Helper
  4. 基于XML及注解配置方式实现AOP及aspectJ表达式
  5. C和指针之字符串编程练习11(统计一串字符包含the的个数)
  6. 95-290-240-源码-内存管理-StreamRecord-StreamRecord简介
  7. [knowledge][basic][hardware] 内存的硬件结构(转)
  8. 年终福利 | “社区之星”(年度贡献者)成长故事征集
  9. scale data:线性空间映射
  10. 接口接收数据_你知道RS232与RS485接口的区别吗?
  11. css hr标签 各种样式
  12. YUV编码为H264 H264封装为MP4
  13. 高通sdx12 audio架构
  14. 计算机类游戏本,游戏本电脑性价比排行2020年推荐 “性价比之最”你知道吗?...
  15. 无线渗透----kismet工具使用
  16. Android 应用的逆向和审计
  17. 模仿人类逻辑,首个BERT模型AI通过初二科学考试!研究人员:完成了老板遗愿...
  18. apache zip java_java中ant包中的org.apache.tools.zip实现压缩和解压缩
  19. 螺旋传动设计系统lisp_螺旋传动设计
  20. ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘ (10061)的解决方法

热门文章

  1. 电脑如何录屏?分享4个屏幕录制的好方法,建议收藏
  2. 批流融合系统-SparkV2/Beam
  3. COUNTIFS函数
  4. 最长上升子序列 CSU - 1047 ( LIS LCS )
  5. 两种操作botton的方法
  6. 线上连锁线下整合的连锁电商架构 打造店店互推人人分销模式
  7. IDEA下,Ctrl+Alt+方向键,屏幕翻转问题
  8. 剑指 Offer 24. 反转链表
  9. PS制作3D立体字、透明气泡
  10. 大数据开发学习脑图+学习路线清晰的告诉你!月薪30K很轻松