leetcode [#463] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (24)
目录1. 题目2. 解决方案3. 注意事项
题目You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and heig...阅读全文

leetcode [#6] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (19)
目录1. 题目2. 解决方案3. 注意事项
题目The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
123P   A   H   NA P L S I I GY   I   RAnd then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
解决方案123456789101112131415...阅读全文

leetcode [#448] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (22)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:Input:[4,3,2,7,8,2,3,1]
Output:[5,6]
解决方案123456789101112131415public class Solution {    public List<Integer> findDisappearedNumbers(int[] nums) {     ...阅读全文

leetcode [#53] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (22)
目录1. 题目2. 解决方案3. 注意事项
题目Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
Examplegiven the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6.
解决方案1234567891011public class Solution {    public int maxSubArray(int[] nums) {        int maxSoFar = nums[0];        int maxEndingHere = nums[0];        for (int i = 1; i < nums.length; i++){            maxEndingHere = Math.max(maxEndingHere + nums[i], nums[i...阅读全文

leetcode [#62] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?

Notem and n will be at most 100.
解决方案1234567891011121314151617181920212223public class Solution {    public int uniquePaths(int m, int n) {        int[][] map...阅读全文