【【Leetcode】125. 验证回文串(Valid Palindrome)】教程文章相关的互联网学习教程文章

Leetcode 6. ZigZag Conversion【代码】

这道题比较简单,观察清楚规律后很快可以写出来。但是用go写的版本,总是答案错误,本地无问题。后用Java重写。func convert(s string, numRows int) string {if numRows == 1 {return s}numChars := make(map[int][]string)size := 2 * numRows - 2for i := 0; i<len(s); i++ {x := s[i : i+1]if i % size < numRows {row := i % sizenumChars[row] = append(numChars[row], x)} else {row := size - i % sizenumChars[row] = app...

<LeetCode>88. 合并两个有序数组【代码】

题目传送阵 class Solution {public void merge(int[] nums1, int m, int[] nums2, int n) {int p=m+n-1; //指向nums1数组的末尾int i=m-1,j=n-1; //分别指向nums1,nums2的最后一个元素while(i>=0 && j>=0){if(nums1[i]>nums2[j]){nums1[p--]=nums1[i--];}else{nums1[p--]=nums2[j--];}//nums1[p--]=nums1[i]>nums2[j]?nums1[i--]:nums2[j--];}while(j>=0){nums1[p--]=nums2[j--];}} }

[leetcode 268]Missing Number

Given an array containing n distinct numbers taken from 0,1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.第一次AC代码...

LeetCode之RemoveElement

题目:Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new length.分析:要求,移除具有n个元素的数组中全部指定的数字,返回删除后的数组长度。看似简单。事实上也能体现一个人的编程水平。解法1是优化后的,解法2是參照网上的STL解法。记录下来。代码:解释一下STL的几个算法。都包...

leetcode:Merge Two Sorted Lists【代码】

1、  Merge two sorted (ascending) linked lists and return it as a new sorted list.  The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null. 2、思路  1、判断两数大小,小的话,插入3、/*** Definition for ListNode.* public class ListNode {* int val;* ListNode n...

Leetcode#83Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.分析,删除重复项,每个元素只出现一次,这里使用hash函数,判断节点值是否出现过public class Solution { public ListNode deleteDuplicates(ListNode head) { Map<Integer,Integer> x=new HashMap<Integer,Integer>(); ListNode H=ne...

LeetCode#1.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. Example:Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. UPDATE (2016/2/13):The return format had been changed to zero-based indices. Please read the above updated description caref...

LeetCode 1689. 十-二进制数的最少数目 模拟 观察规律【代码】

地址 https://leetcode-cn.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/如果一个十进制数字不含任何前导零,且每一位上的数字不是 0 就是 1 ,那么该数字就是一个 十-二进制数 。例如,101 和 1100 都是 十-二进制数,而 112 和 3001 不是。给你一个表示十进制整数的字符串 n ,返回和为 n 的 十-二进制数 的最少数目。 示例 1:输入:n = "32" 输出:3 解释:10 + 11 + 11 = 32 示例 2:输入:n = "82...

[LeetCode] 769. Max Chunks To Make Sorted【代码】

Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into some number of "chunks" (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array. What is the most number of chunks we could have made? Example 1: Input: arr = [4,3,2,1,0] Output: 1 Explanation: Splitting into two or more chunks will not return the r...

[LeetCode] Insert Interval【代码】

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].Example 2:Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].This is because the new inter...

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)【代码】【图】

背景(以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路。记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索。等把这一套盒子都打开完,再打开第二套的。Wikipedia上的讲解是:“Depth-first search (DF...

Leetcode 121 Best Time to Buy and Sell Stock

class Solution:# @param {integer[]} prices# @return {integer}def maxProfit(self, prices):if len(prices) <= 1: return 0low = prices[0]; mostProfit = 0for i in range(1, len(prices)):if prices[i] < low: low = prices[i]mostProfit = max(mostProfit, prices[i] - low)return mostProfit版权声明:本文为博主原创文章,未经博主允许不得转载。原文:http://blog.csdn.net/andrew9tech/article/details/46756321

leetcode 之 Minimum Path Sum【代码】

题目描述:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.给定一个m*n的矩形a, 计算从a[0][0]到a[m][n]的每个数字加起来最小的和. 注意, 每次只能往下或者右查找下一个数字.分析:每次都往右下角寻找. 之前看到一道题, 计算n*n的方形从左上角到又下角...

leetcode 241 加优先级括号【代码】

核心思想:逐渐遍历,如果出现符号则分为左右两侧,用于后续分治两侧数值已经确定好之后再确定最开始的符号并计算最终的结果如果已经只有一个数字,没有运算符号,则把这个数字直接加入到结果集中注意每一次递归都定义了一个List,也就是在递归的结束都会把这个值返回给上一步的结果,用于最终的值的计算。class Solution {public List<Integer> diffWaysToCompute(String input) {int n=input.length();List<Integer> res= new Ar...

Leetcode 120【代码】

class Solution { public:int minimumTotal(vector<vector<int>>& triangle) {if(triangle.size() == 1) return triangle[0][0];triangle[1][0] += triangle[0][0];triangle[1][1] += triangle[0][0];if(triangle.size() == 2) return min(triangle[1][0],triangle[1][1]);int a = min(triangle[1][0],triangle[1][1]);for(int i=0;i < 2;i++){triangle[2][i] += a;}triangle[2][2] += triangle[1][1];for(int i=3;i < triangle.si...