当前位置:   article > 正文

Leecode编程题,刷题,笔试题_编程刷题 code

编程刷题 code

Lee.第1题:

方法一:暴力破解法:在原数组上进行操作。

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. for(int i = 0; i < nums.length - 1; i++) {
  4. for(int j = i + 1; j < nums.length; j++) {
  5. if(nums[i] + nums[j] == target) {
  6. return new int[] {i,j};
  7. }
  8. }
  9. }
  10. return null;
  11. }
  12. }

解析:定义一个 i 从数组的下标0开始遍历,定义一个 j 从 i + 1开始遍历,先遍历j ,判断 i 对应的值加 j 对应的值是否等于 target的值,若等于,则返回下标,不等于让 i ++,i 遍历完之后,若没找到,则返回null,代表没有符合要求的数字。

方法二:哈希表法:

解析:用到HashMap,给定target的值,用target - i 对应的值,i 从数组下标0处开始,i 对应的值一开始为11,做差后在HashMap中找不到,就把i对应的值和角标存到HashMap中,一次遍历做差,直到角标3处,9 - 7 = 2,可以在HashMap中找到2,所以返回7和2对应的角标即可。代码如下:

Lee.第二题:三数之和

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。注意:答案中不可以包含重复的三元组。

方法一:双指针

  1. public class threeSum {
  2. public List<List<Integer>> threeSum(int[] nums) {
  3. int n = nums.length;
  4. List<List<Integer>> list1 = new ArrayList<List<Integer>>();
  5. Arrays.sort(nums);
  6. for (int i = 0; i < nums.length; i++){
  7. if (nums[i] > 0){
  8. return list1;
  9. }
  10. if (i > 0 && nums[i] == nums[i - 1]){
  11. continue;
  12. }
  13. int cur = nums[i];
  14. int L = i + 1;
  15. int R = nums.length - 1;
  16. while (L < R){
  17. int temp = cur + nums[L] + nums[R];
  18. if (temp == 0){
  19. List<Integer> list = new ArrayList<>();
  20. list.add(cur);
  21. list.add(nums[L]);
  22. list.add(nums[R]);
  23. list1.add(list);
  24. L++;
  25. R--;
  26. while (L < R && nums[L] == nums[L - 1]){
  27. L++;
  28. }
  29. while (L < R && nums[R] == nums[R + 1])
  30. R--;
  31. }else if (temp > 0){
  32. R--;
  33. }else {
  34. L++;
  35. }
  36. }
  37. }
  38. return list1;
  39. }
  40. }

(3):统计n以内的素数,素数是只能被1和自身整除的数,0,1除外;

方法一:暴力破解法

  1. public class lianxi {
  2. public static void main(String[] args) {
  3. System.out.println(countSuNum(100));
  4. }
  5. public static int countSuNum(int n){
  6. int count = 0;
  7. for (int i = 2; i < n; i++){
  8. count += isPrima(i) ? 1 : 0;
  9. }
  10. return count;
  11. }
  12. private static boolean isPrima(int x) {
  13. for (int i = 2; i < x; i++){
  14. if (x % i == 0){
  15. return false;
  16. }
  17. }
  18. return true;
  19. }
  20. }

方法二:埃筛法,是bf的一种优化;(eratosthenes)

  1. public class lianxi {
  2. public static void main(String[] args) {
  3. System.out.println(eratasthenes(100));
  4. }
  5. public static int eratasthenes(int n){
  6. boolean[] isPrime = new boolean[n];
  7. int count = 0;
  8. for (int i = 2; i < n; i++){
  9. if (!isPrime[i]){
  10. count++;
  11. for (int j = 2 * i; j < n; j += i){
  12. isPrime[j] = true;
  13. }
  14. }
  15. }
  16. return count;
  17. }
  18. }

需要注意的是内层for循环,i是外层的变量,后面用j+=i让j自加;

(4):链表反转,将链表的顺序反转过来

方法一:递归

        递归最关键的就是把大的问题华为小的问题解决,最后把这个方法用到整个问题当中;

遇到问题不要慌,先分析问题:

配图一:结点反转的思路;

配图2:从后向前反转,头结点的确定;

 (2):迭代法:

从前向后,先创建一个next保存下一个节点的值,即head.next,将head.next置空,再保存head的值再prev中,用于第二个结点指向第一个结点,再把next的值赋给head,让head走到第二个结点

全部代码如下:

  1. public class lianxi {
  2. static class ListNode{
  3. int val;
  4. ListNode next;
  5. public ListNode(int val,ListNode next){
  6. this.val = val;
  7. this.next = next;
  8. }
  9. }
  10. //迭代
  11. public static ListNode iterate(ListNode head){
  12. ListNode prev = null,next;
  13. ListNode curr = head;
  14. while (head != null){
  15. next = curr.next;
  16. curr.next = prev;
  17. prev = curr;
  18. curr = next;
  19. }
  20. return prev;
  21. }
  22. //递归
  23. public static ListNode recursion(ListNode head){
  24. if (head == null || head.next == null){
  25. return head;
  26. }
  27. ListNode new_node = recursion(head.next);
  28. head.next.next = head;
  29. head.next = null;
  30. return new_node;
  31. }
  32. public static void main(String[] args) {
  33. ListNode node5 = new ListNode(5,null);
  34. ListNode node4 = new ListNode(4,node5);
  35. ListNode node3 = new ListNode(3,node4);
  36. ListNode node2 = new ListNode(2,node3);
  37. ListNode node1 = new ListNode(1,node2);
  38. // ListNode prev = iterate(node1);
  39. // System.out.println(prev);
  40. ListNode prev = iterate(node1);
  41. System.out.println(prev);
  42. }
  43. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/417702
推荐阅读
相关标签
  

闽ICP备14008679号