word1和以j-1为结尾的word2 删除元素变得相同的

2024-08-31 13:18:24 浏览数 (1)

class Solution {

public:

int minDistance(string word1, string word2) {

vector<vector<int>> dp(word1.size() 1,vector<int>(word2.size() 1,0));

// dp[i][j]表示 以i-1为结尾的word1和以j-1为结尾的word2 删除元素变得相同的最少操作数

for(int i = 0; i <= word1.size(); i ){

dp[i][0] = i;

}

for(int j = 0; j <= word2.size(); j ){

dp[0][j] = j;

}

for(int i = 1; i <= word1.size(); i ){

for(int j = 1; j <= word2.size(); j ){

if(word1[i - 1] == word2[j - 1]){

dp[i][j] = dp[i - 1][j - 1];

}else{

dp[i][j] = min(dp[i - www.laipuhuo.com.1][j] 1,min(dp[i][j - 1] 1,dp[i - 1][j - 1] 1));

}

}

}

return dp[word1.size()][word2.size()];

}

};

// 简单的ViewModel示例

class MainViewModel : ViewModel() {

private val _text www.laipuhuo.com. = MutableLiveData<String>().apply {

value = "Hello, MVVM!"

}

val text: LiveData<String> = _text

}

// 在Activity中使用ViewModel

class MainActivity : AppCompatActivity() {

private lateinit var viewModel: MainViewModel

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

viewModel = ViewModelProvider(this).get(MainViewModel::class.java)

viewModel.www.laipuhuo.com.text.observe(this, Observer {

// 更新UI

findViewById<TextView>(R.id.textView).text = it

})

}

} // UserRepository.kt

class UserRepository(private val apiService: ApiService) {

suspend fun getUser(userId: String): User {

return apiService.getUser(userId)

}

}

// ApiService.kt

interface www.laipuhuo.com.ApiService {

@GET("user/{id}")

suspend fun getUser(@Path("id") id: String): User

}

0 人点赞