Kokumi

导航

Leecode

1. 两数之和

题目:

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

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

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

预期:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]

代码如下:

public class Solution {
    public int[] TwoSum(int[] nums, int target)
        {
            int[] result = null;  // 定义最后要输出的新数组
            int b = nums.Length; // 输入数组的长度,即数组里有几个值
           
            for (int i = 0; i < b; i++) // 第一层嵌套,i是为了进行比较两个值的大小所设定的第一个值
            {
                for (int a = i + 1; a < b; a++) // 第二层嵌套,a是和i比较的第二个值,a的初始值比i+1是为了避免重复比较。
                {
                    if (nums[i] + nums[a] == target) // 两个值相加等于target
                    {
                        result = new int[] { i, a };  // 将两个字添加到新数组,注意,这里添加的i和a是数组的索引。
                    } 
                }
            }
                return result; 
        }
}
9.回文数
题目:

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

例如,121 是回文,而 123 不是。

代码如下:

public class Solution {
    public bool IsPalindrome(int x) {
        bool result = false;
        string xnum = x.ToString();
        string res = null;
        if(xnum.Length > 1){
            for(int i = 1; i < xnum.Length; i++){
                res += xnum[xnum.Length-i];
                if(i == xnum.Length-1){
                    res += xnum[0];
                }
            }
            if(res == xnum){
                result = true;
            }
        }else if(xnum.Length == 1){
            result = true;
        }
        return result;
    }
}
 
 

14.最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串 ""。

 

示例 1:

输入:strs = ["flower","flow","flight"]
输出:"fl"
示例 2:

输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。
 

提示:

1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成

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

 
public class Solution {
    
    public string LongestCommonPrefix(string[] strs) {
        string[] compare = new string[strs[0].Length];
        string[] CompareIn = new string[strs.Length];
        string result = "";
            string Comindex = "";
            for (int i = 0; i < strs[0].Length; i++)
            {
                string index = strs[0].Substring(0, i + 1);
                compare[i] = index;
            }
            for (int j = 0; j < compare.Length; j++)
            {
                Comindex = "0";
                // 初始数组的值依次和第一个值的所有前缀进行比较,存进CompareIn
                for (int i = 0; i < strs.Length; i++)
                {
                    if (i != 0)
                    {
                        string res = CompareIndex(compare[j], strs[i]);
                        CompareIn[i] = res;
                    }
                    else
                    {
                        CompareIn[0] = "true";
                    }
                }
                foreach (var item in CompareIn)
                {
                    
                    if (item == "false")
                    {
                        Comindex = "1";
                    }
                }
                if (Comindex != "1")
                {
                    result = compare[j];
                }

            }
        return result;
    }
    public string CompareIndex(string compare,string strs){
        if(strs.StartsWith(compare)){
            return "true";
        }else{
            return "false";
        }
    }
}
 

28.实现 strStr() 

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回  -1 。

说明

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。

代码:
public class Solution {
    public int StrStr(string haystack, string needle) {
        if(needle != null || needle != ""){
            if(haystack.Contains(needle)){
                int result = 0 ;
                for(int i = 0;i < haystack.Length; i++){
                    if(i+needle.Length <= haystack.Length){
                        if(haystack.Substring(i,needle.Length).ToString() == needle){
                        result = i;
                        break;
                    }
                }
            }
            return result;
            }else{
                return -1;
            }
        }else{
            return 0;
        }
    }
}

posted on 2021-08-11 10:58  Kokumi  阅读(51)  评论(0)    收藏  举报